Changing variable type of R dataframe inside a list -


i have list of dataframes , need transform variable in each of dataframes factor.

e.g.

 mylist <- list(df1 = data.frame(a = sample(10), b = rep(1:2, 10)),                 df2 = data.frame(a = sample(10), b = rep(1:2, 10))                 ) 

lets variable b needs factor in each dataframe. i've tried this:

 tmp <- setnames(lapply(seq_along(mylist), function(x) apply(mylist[[x]][c("b")], 2, factor)), names(mylist)) 

but returns transformed variable, not whole dataframe need. know how loop, don't want resort that.

per comment david arenburg, solution should work:

tmp <- lapply(mylist, function(x) {x[, "b"] <- factor(x[, "b"]) ; x}) ; str(tmp) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -