[R] R as.numeric()

From: Lutz Fischer <lfischer_at_staffmail.ed.ac.uk>
Date: Tue, 24 May 2011 22:20:48 +0100


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.

______________________________________________
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.
Received on Tue 24 May 2011 - 21:29:10 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 24 May 2011 - 21:50:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive