[R] Apply as.factor (or as.numeric etc) to multiple columns

hadley wickham h.wickham at gmail.com
Tue Jun 23 23:36:49 CEST 2009


Hi Mark,

Have a look at colwise (and numcolwise and catcolwise) in the plyr package.

Hadley

On Tue, Jun 23, 2009 at 4:23 PM, Mark Na<mtb954 at gmail.com> 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 at 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.
>



-- 
http://had.co.nz/




More information about the R-help mailing list