[R] read.table and trouble
Andy Bunn
abunn at montana.edu
Sat Apr 6 22:41:01 CEST 2002
Hi all,
I'm porting a library I wrote in S-plus over to R. All but one of the
functions work fine. The only one that doesn't is really giving me a
headache. I'm admitting defeat. Can anybody help?
Object example3 has been read in using read.table. The statement works fine
in S-Plus.
When I run the same statement in R the column Prefix, which is read in using
the
as.character coercion, is wacky. It reads in as numeric.
> example3
nRows nWaves nYears Noise Amp Freq AmpTwo FreqTwo Prefix
1 1 1 1000 0.02 0.01 2 NA NA "test"
2 2 1 1000 0.02 0.02 2 NA NA "test2"
3 3 1 1000 0.02 0.03 2 NA NA NA
4 4 1 1000 0.02 0.04 2 NA NA NA
5 5 1 1000 0.02 0.05 2 NA NA NA
6 6 1 1000 0.02 0.06 2 NA NA NA
7 7 1 1000 0.02 0.07 2 NA NA NA
8 8 1 1000 0.02 0.08 2 NA NA NA
9 9 1 1000 0.02 0.09 2 NA NA "test3"
10 10 1 1000 0.02 0.10 2 NA NA NA
11 11 2 1000 0.02 0.01 2 0.01 5 NA
12 12 2 1000 0.02 0.02 2 0.02 5 NA
13 13 2 1000 0.02 0.03 2 0.03 5 NA
14 14 2 1000 0.02 0.04 2 0.04 5 NA
15 15 2 1000 0.02 0.05 2 0.05 5 NA
16 16 2 1000 0.02 0.06 2 0.06 5 NA
17 17 2 1000 0.02 0.07 2 0.07 5 NA
18 18 2 1000 0.02 0.08 2 0.08 5 NA
19 19 2 1000 0.02 0.09 2 0.09 5 NA
20 20 2 1000 0.02 0.10 2 0.10 5 NA
> attach(example3)
> Prefix[1]
[1] 3
> Prefix[2]
[1] NA
> is.numeric(Prefix[1])
[1] TRUE
> is.character(Prefix[1])
[1] FALSE
Here's the read.table command:
DriverTable <- read.table(file = DriverFile, header = T, sep = ",",
row.names = NULL, na.strings = "NA")
I then pass a bunch of arguments to another function like so:
for(i in 1:length(nRows))
{
RingSim(nWaves[i], nYears[i], Noise[i], Amp[i], Freq[i], AmpTwo[i],
FreqTwo[i], as.character(Prefix[i]))
}
This works in S. All the variables (nWaves, nYears, etc.) are numeric except
Prefix. How can I read Prefix in correctly - that is as a string.
Here's the file that is read in:
nRows, nWaves, nYears, Noise, Amp, Freq, AmpTwo, FreqTwo, Prefix
1,1,1000,0.02,0.01,2,NA,NA,test1
2,1,1000,0.02,0.02,2,NA,NA,test2
3,1,1000,0.02,0.03,2,NA,NA,NA
4,1,1000,0.02,0.04,2,NA,NA,test3
5,1,1000,0.02,0.05,2,NA,NA,NA
6,1,1000,0.02,0.06,2,NA,NA,NA
7,1,1000,0.02,0.07,2,NA,NA,NA
8,1,1000,0.02,0.08,2,NA,NA,NA
9,1,1000,0.02,0.09,2,NA,NA,test4
10,1,1000,0.02,0.1,2,NA,NA,NA
11,2,1000,0.02,0.01,2,0.01,5,NA
12,2,1000,0.02,0.02,2,0.02,5,NA
13,2,1000,0.02,0.03,2,0.03,5,NA
14,2,1000,0.02,0.04,2,0.04,5,NA
15,2,1000,0.02,0.05,2,0.05,5,NA
16,2,1000,0.02,0.06,2,0.06,5,NA
17,2,1000,0.02,0.07,2,0.07,5,NA
18,2,1000,0.02,0.08,2,0.08,5,NA
19,2,1000,0.02,0.09,2,0.09,5,NA
20,2,1000,0.02,0.1,2,0.1,5,NA
Why on earth is everything read in correctly except the Prefix column? Why
does Prefix[1] return 3 and the rest of the column is NA?
Thanks in advance,
Andy
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list