[R] read.table and trouble

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sat Apr 6 23:41:27 CEST 2002


"Andy Bunn" <abunn at montana.edu> writes:

> > 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")
> 
...
> 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
...
> 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?

It reads as expected for me. Are you sure there isn't another Prefix
variable around? (attach() attaches at position 2, *after* the global
environment). 

> zz <- read.csv("xyzzy.dat") # should suffice
> attach(zz)
> Prefix[1]
[1] test1
Levels:  test1 test2 test3 test4 
>  Prefix[2]
[1] test2
Levels:  test1 test2 test3 test4 
> is.numeric(Prefix[1])
[1] FALSE
>  is.character(Prefix[1])
[1] FALSE

(notice that it is a factor, not an character vector).

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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