Wacek helped me out on a similar topic a while back,
ize = function (d, columns = names(d), izer = as.factor) { d[columns] = lapply(d[columns], izer) d } d = data.frame(x=1:10, y=1:10, z =1:10) str( ize(d, 'y') ) # y is now a factor str( ize(d, 1:2, `cumsum`) ) # x and y are affected etc. HTH, baptiste Mark Na wrote:
Hi R-helpers, I have a dataframe with 60columns and I would like to convert several columns to factor, others to numeric, and yet others to dates. Rather than having 60 lines like this: data$Var1<-as.factor(data$Var1) I wonder if it's possible to write one line of code (per data type, e.g. factor) that would apply a function (e.g., as.factor) to several (non-contiguous) columns. So, I could then use 3 or 4 lines of code (for 3 or 4 data types) instead of 60. I have tried writing an apply function, but it failed. Thanks for any help you might be able to provide. Mark Na ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
-- _____________________________ Baptiste AuguiƩ School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.