Reputation: 3594
In the spirit of "no question is too simple" , how do I save the output of an lapply
function to the list on which it was applied. For e.g. I am doing
lapply(listofdf, function(x) as.numeric(gsub("[$,]", "", x[ ,18])))
where x is data frame and gsub
is applied to 18th column. I want to save the result to the 18th column in the listofdf itself. I've never worked with lists. with a data frame we would simply do
df$whatever <- function(df$whatever, .... )
. With a list
how would this work?
Upvotes: 1
Views: 2185
Reputation: 886938
You could do
listofdf <- lapply(listofdf, function(x){
x[,18] <- as.numeric(gsub("[$,]", "", x[ ,18]))
#assign 18th column to the modified column
x #return the dataframe
})
Upvotes: 4