0

I used the assign() function to create 20 data frames and assign them with content using read.csv() and a for loop like follows

data_names=paste0("data_",countries$country)


for (i in 1:dim(data_names)[1])
 {
   assign(data_names[i], read.csv(file=files[i],
                header = TRUE, 
                sep = ',',
                colClasses=rep("character",16),
                na.strings='NULL'
                ))
 }

Where countries$country is a vector of 20 different names of countries.

Now, for each of these data frames, I want to convert columns 5 to 14 to integer. Manually, I would do like this:

data_russia      [,5:14]<- sapply(data_russia     [,5:14],as.integer)            
data_belarus     [,5:14]<- sapply(data_belarus    [,5:14],as.integer)     
data_philippines [,5:14]<- sapply(data_philippines[,5:14],as.integer)            
data_srilanka    [,5:14]<- sapply(data_srilanka   [,5:14],as.integer)        
data_germany     [,5:14]<- sapply(data_germany    [,5:14],as.integer)         
data_serbia      [,5:14]<- sapply(data_serbia     [,5:14],as.integer)      
data_croatia     [,5:14]<- sapply(data_croatia    [,5:14],as.integer)      
data_libya       [,5:14]<- sapply(data_libya      [,5:14],as.integer)      
data_burma       [,5:14]<- sapply(data_burma      [,5:14],as.integer)    
data_algeria     [,5:14]<- sapply(data_algeria    [,5:14],as.integer)      
data_bosnia      [,5:14]<- sapply(data_bosnia     [,5:14],as.integer)      
data_bulgaria    [,5:14]<- sapply(data_bulgaria   [,5:14],as.integer)     
data_iraq        [,5:14]<- sapply(data_iraq       [,5:14],as.integer)      
data_hungary     [,5:14]<- sapply(data_hungary    [,5:14],as.integer)   
data_greece      [,5:14]<- sapply(data_greece     [,5:14],as.integer)   
data_usa         [,5:14]<- sapply(data_usa        [,5:14],as.integer)   
data_vietnam     [,5:14]<- sapply(data_vietnam    [,5:14],as.integer)    
data_ukraine     [,5:14]<- sapply(data_ukraine    [,5:14],as.integer)     
data_france      [,5:14]<- sapply(data_france     [,5:14],as.integer)     
data_egypt       [,5:14]<- sapply(data_egypt      [,5:14],as.integer)   

How would you perform the same action sequentially over the names of countries?

Thanks in Advance for your pieces of advice!

Ronicho
  • 1
  • 1
  • 2
    [Put your data frames in a list](https://stackoverflow.com/a/24376207/903061). Then, `for (i in seq_along(data_list)) data_list[[i]][, 5:14] = sapply(data_list[[i]][, 5:14], as.integer)` – Gregor Thomas Dec 04 '19 at 17:04
  • 1
    @Gregor's comment on a `list`-of-frames also suggests replacing his `for` loop with `data_list – r2evans Dec 04 '19 at 17:24
  • Thanks @Gregor and for your perfect answers! – Ronicho Dec 05 '19 at 10:23

0 Answers0