Hi, I'm trying to figure out if there's an automated way to get read.table to read in my data and *not* convert the character columns into anything, just leave them alone. What I'm referring to as 'character columns' are columns in the data that are quoted. For columns of alphabetic strings (that aren't TRUE or FALSE) I can suppress conversion to factor with as.is=TRUE, but what I'd like to stop is the conversion of quoted numbers of the form "01","02",..., into numeric form. By an 'automated way', I mean one that does not involve me having to know which columns in the data are the ones I want kept as they are. This doesn't seem like an unreasonable thing to want to do. After all, say I've got the data.frame: A <- data.frame(a=1:3, b=I(c("01","02","03"))) I can export this to a text file with the simple command write.table(A, "A.txt", sep="\t", row.names=FALSE, quote=TRUE) but I cannot find an equally simple mechanism for reading this data back in from A.txt that allows me to reconstruct my data.frame 'A'. Is this an unreasonable thing to expect? Thanks, Doug
On Thu, 26 Jan 2006, Douglas Grove wrote:> Hi, > > I'm trying to figure out if there's an automated way to get > read.table to read in my data and *not* convert the character > columns into anything, just leave them alone. What I'm referring > to as 'character columns' are columns in the data that are quoted. > For columns of alphabetic strings (that aren't TRUE or FALSE) I can > suppress conversion to factor with as.is=TRUE, but what I'd like to > stop is the conversion of quoted numbers of the form "01","02",..., > into numeric form.One approach is to use quote="" to specify that the quote marks are considered part of the string. Depending on what you are using the data for, you may then need to strip the quotes off, eg with sub().> B<-read.table("a.txt",sep="\t",as.is=TRUE,header=TRUE,quote="") > BX.a. X.b. 1 1 "01" 2 2 "02" 3 3 "03"> sub("^\"(.*)\"$", "\\1", B[[2]])[1] "01" "02" "03" -thomas
Douglas Grove wrote:> Hi, > > I'm trying to figure out if there's an automated way to get > read.table to read in my data and *not* convert the character > columns into anything, just leave them alone. What I'm referring?Did you read the help page? What about argument as.is=TRUE? See also argument colClasses Kjetil> to as 'character columns' are columns in the data that are quoted. > For columns of alphabetic strings (that aren't TRUE or FALSE) I can > suppress conversion to factor with as.is=TRUE, but what I'd like to > stop is the conversion of quoted numbers of the form "01","02",..., > into numeric form. > > By an 'automated way', I mean one that does not involve me having > to know which columns in the data are the ones I want kept as > they are. > > This doesn't seem like an unreasonable thing to want to do. > After all, say I've got the data.frame: > > A <- data.frame(a=1:3, b=I(c("01","02","03"))) > > I can export this to a text file with the simple command > > write.table(A, "A.txt", sep="\t", row.names=FALSE, quote=TRUE) > > but I cannot find an equally simple mechanism for reading this > data back in from A.txt that allows me to reconstruct my > data.frame 'A'. Is this an unreasonable thing to expect? > > Thanks, > Doug > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >