David Kane <David Kane
2001-Jul-11 12:35 UTC
[R] Forcing variables types when using read.table
Perhaps I am being thick, but I can not figure out how to force read.table to treat as character something that "looks" numeric to it. Here is a simple example:> version_ platform sparc-sun-solaris2.6 arch sparc os solaris2.6 system sparc, solaris2.6 status major 1 minor 3.0 year 2001 month 06 day 22 language R> file.show("temp.txt")sedol,price 000312,47 071290,112> read.table("temp.txt", header = TRUE, sep = ",")sedol price 1 312 47 2 71290 112> read.table("temp.txt", header = TRUE, sep = ",", as.is = TRUE)sedol price 1 312 47 2 71290 112>Note that "sedol" is really a character string (it is an international identifier for stocks) while price is numeric. How can I get read.table to create sedol as character (and therefore keep around the leading zeroes)? I guess that I am looking for an option like "what" from scan . . . Thanks, Dave Kane -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 11-Jul-2001 David Kane <David Kane wrote:> Perhaps I am being thick, but I can not figure out how to force read.table to > treat as character something that "looks" numeric to it. Here is a simple > example: > >> version > _ > platform sparc-sun-solaris2.6 > arch sparc > os solaris2.6 > system sparc, solaris2.6 > status > major 1 > minor 3.0 > year 2001 > month 06 > day 22 > language R >> file.show("temp.txt") > sedol,price > 000312,47 > 071290,112 > >> read.table("temp.txt", header = TRUE, sep = ",") > sedol price > 1 312 47 > 2 71290 112 >> read.table("temp.txt", header = TRUE, sep = ",", as.is = TRUE) > sedol price > 1 312 47 > 2 71290 112 >> > > Note that "sedol" is really a character string (it is an international > identifier for stocks) while price is numeric. How can I get read.table to > create sedol as character (and therefore keep around the leading zeroes)? I > guess that I am looking for an option like "what" from scan . . .You can just use scan instead of read.table: x <- scan("temp.txt", what=list("",0), skip=1, sep=",") names(x) <- scan("temp.txt", what="", nlines=1, sep=",") as.data.frame(x) This will convert sedol to a factor variable, which seems appropriate. Martyn -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._