[R] Changing variable class problems

Joshua Wiley jwiley.psych at gmail.com
Thu Sep 16 01:25:45 CEST 2010


Hi Richard,

Something like workf[ , columns] <- data.frame(lapply(workf[,
columns], as.numeric)) should do what you're after.  However, this is
really a bit of a work around to the real problem.  Can you provide
more details on the csv file you are reading in?  Perhaps the first
couple rows or something?  Something in those columns is making R
think those variables are not numeric in the first place, and
correcting this will circumvent the need to use as.numeric()
altogether.

Best regards,

Josh


On Wed, Sep 15, 2010 at 4:09 PM, Richard Barfield <rich.bgjla at gmail.com> wrote:
> I'm reading in some data from a csv file, and it's reading in some of the
> columns as character variables instead of numeric. I know I can fix this by
> doing as.numeric for each of the columns, but the problem is I have a LOT of
> different quantitative variables that I would have to do this for.
> I've been trying something along the following but it hasn't really been
> working
>>columns<-c(1,4,6,8,20,22) # where these are the columns that are numeric
> workf is the data matrix that I'm working with
> So I tried
>>workf[,columns]<-as.numeric[,columns]
> when I do this I get the error message
> Error: (list) object cannot be coerced to type 'double'
>
> I've tried doing it different ways, with a new data matrix Matr and doing
>>Matr<-workf
>>Matr[,columns]<-as.numeric[,columns]
>
> but it keeps on bringing up the same error message. Could someone help me
> with this? Thanks
> Sincerely,
> Richard
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list