[R] R as.numeric()
Lutz Fischer
lfischer at staffmail.ed.ac.uk
Tue May 24 23:20:48 CEST 2011
Hi,
I have a bit of a problem with as.numeric or as.double.
I read in an excel-file (either xlsx::read.xlsx2 or gdata::read.xls).
Select a subset and then try to make it numeric:
# read in the excel-file
alldata<-read.xlsx2("input.xls",1)
# select the subset
s<-subset(alldata, select=c("cI","cII","cIII","cIV","cV"))
# unluckily we have "n/a" for missing values in the file - so we turn it
into "proper" missing values
s[s == "n/a"]<-NA
n<-data.matrix(s);
The problem I have is that it does not convert the date the way I would
expect.
just as an example:
> s[1,2]
[1] 30.94346629
3136 Levels: 0.026307482 0.028239812 0.02849896 0.029054564 0.029540352
0.030248034 0.030841352 0.032966308 ... n/a
turned into:
> n[1,2]
[1] 3020
And I would like to have there 30.94346629 as well. I assume that has to
do with the "Levels" attribute - but not sure what to make of these in
the first place.
I also tried to convert each value on its own:
#make some space that holds the actual numeric data
n <- array(dim=c(length(s[,1]),length(s)))
# now turn everything into doubles
for (c in 1:length(s)) {
for (r in 1:length(s[,1])) {
n[r,c]<-as.double(s[r,c])
}
}
but that gave the same result - just a lot slower.
Thanks
Lutz
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
More information about the R-help
mailing list