[R] as.integer

Greg Snow Greg.Snow at imail.org
Mon Jun 7 22:58:30 CEST 2010

Usually when read.table converts numbers to factors it means that there is a problem in the data file, an extra character somewhere.  It is best to fix the problem in the source data (or a copy of the source data) so that the data imports properly rather than try to fix it post hoc.  If that is not an option, then you can specify the colClasses argument to read.table to make sure that it reads the data into the type of variable that you want.  If you really need the post read.table solution, then lapply will run the same function (as.integer) on each column of a data frame (element of a list).

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of aledanda
> Sent: Monday, June 07, 2010 8:05 AM
> To: r-help at r-project.org
> Subject: [R] as.integer
> Hi,
> Is there a way to recursively change the variables imported with
> read.table(..) - and then attached with attach(..) - from as.factor to
> as.integer?
> I have about 9 variables that are now recognized as factors but I need
> to do
> statistics on them so I need to change them in as.integer. It works,
> but I
> wonder if there is a way to to this for all the variables in one go.
> Thanks a lot for your help
> Ale
> --
> View this message in context: http://r.789695.n4.nabble.com/as-integer-
> tp2245987p2245987.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> 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.

More information about the R-help mailing list