This is not so hard. Yoy have to loop (lapply
) through the list changing each member data.frame's columns of interest with another call to lapply
.
First, some example dataset, since you have posted none.
set.seed(1234)
myfiles2 <- lapply(1:4, function(i){
data.frame(X = letters[1:5], Y = factor(sample(5)), Z = as.character(11:15))
})
str(myfiles2) # output omited
Now, the problem.
cols <- 2:3
myfiles3 <- lapply(myfiles2, function(DF){
DF[cols] <- lapply(DF[cols], as.numeric)
DF
})
To see that it worked any of the following two will do.
lapply(myfiles3, function(DF) sapply(DF, class))
str(myfiles3)
#List of 4
# $ :'data.frame': 5 obs. of 3 variables:
# ..$ X: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
# ..$ Y: num [1:5] 1 3 2 4 5
# ..$ Z: num [1:5] 1 2 3 4 5
# $ :'data.frame': 5 obs. of 3 variables:
# ..$ X: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
# ..$ Y: num [1:5] 4 1 5 2 3
# ..$ Z: num [1:5] 1 2 3 4 5
# $ :'data.frame': 5 obs. of 3 variables:
# ..$ X: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
# ..$ Y: num [1:5] 4 3 1 2 5
# ..$ Z: num [1:5] 1 2 3 4 5
# $ :'data.frame': 5 obs. of 3 variables:
# ..$ X: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
# ..$ Y: num [1:5] 5 2 1 3 4
# ..$ Z: num [1:5] 1 2 3 4 5