[R] as.numeric() generates NAs inside an apply call, but fine outside of it
peter dalgaard
pdalgd at gmail.com
Mon Jan 9 15:29:03 CET 2012
On Jan 9, 2012, at 15:11 , Chris Beeley wrote:
> Hello-
>
> I have rather a messy SPSS file which I have imported to R, I've dput'd some of the columns at the end of this message. I wish to get rid of all the labels and have numeric values using as.numeric. The funny thing is it works like this:
>
> as.numeric(mydata[,2]) # generates correct numbers
>
> however, if I pass the whole dataframe at once like this:
>
> apply(mydata, 1:2, function(x) as.numeric(x))
This is your problem. apply(mydata,....) implies as.matrix(mydata) and that turns everything to character, and in the case of a factor column that means the _levels_. I.e., this effect:
> as.matrix(mydata)
id item2.jan11 item12.jan11
[1,] " 1" "quite a lot" " 5"
[2,] " 2" "somewhat" " 5"
[3,] " 3" "missing data" "999"
[4,] " 4" "quite a lot" " 5"
....
You might be looking for
as.data.frame(lapply(mydata, as.numeric))
--
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list