karena
2010-Aug-05 04:32 UTC
[R] a question about 'read.table' with or without 'read.table'.(urgent)
Hi, I've got a quite tricky question. I have a txt file, named 'temp.txt', as the following: snp1 snp2 snp3 AA 00 00 GG GG 00 00 AA 00 I want to read the file into R. 1) when I use 'read.table' without 'header=T' option,> temp <- read.table('temp.txt')# I got> tempV1 V2 V3 1 snp1 snp2 snp3 2 AA 00 00 3 GG GG 00 4 00 AA 00 2) If I include the 'header=T' option,> temp <- read.table('temp.txt', header=T, as.is=T)# I got> tempsnp1 snp2 snp3 1 AA 00 0 2 GG GG 0 3 00 AA 0 The difference is for 'snp3', we can see, in 1) the values for snp3 are all '00', while in 2) the values are all '0'. How can I keep the original values for snp3 as '00', meanwhile get the 'headers' or say the colnames as 'snp1 snp2 snp3'? thank you very much, karena -- View this message in context: http://r.789695.n4.nabble.com/a-question-about-read-table-with-or-without-read-table-urgent-tp2314423p2314423.html Sent from the R help mailing list archive at Nabble.com.
Wu Gong
2010-Aug-05 05:17 UTC
[R] a question about 'read.table' with or without 'read.table'.(urgent)
read.table("temp.txt",header=T,colClasses = "character") ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/a-question-about-read-table-with-or-without-read-table-urgent-tp2314423p2314445.html Sent from the R help mailing list archive at Nabble.com.
John Kane
2010-Aug-05 13:54 UTC
[R] a question about 'read.table' with or without 'read.table'.(urgent)
I believe Wu Gong has given you a solution. As a note you were probably reading in the first two columns as either factors or characters and the last one as numeric. You might want to try it again and then do a str() on the resulting data.frame to see what was happening. It can be confusing to discover that you have factor rather than character data in the data.frame. :) --- On Thu, 8/5/10, karena <dr.jzhou at gmail.com> wrote:> From: karena <dr.jzhou at gmail.com> > Subject: [R] a question about 'read.table' with or without 'read.table'.(urgent) > To: r-help at r-project.org > Received: Thursday, August 5, 2010, 12:32 AM > > Hi, I've got a quite tricky question. > I have a txt file, named 'temp.txt', as the following: > snp1? ? ? ? snp2? ? ? > ? snp3 > AA? ? ? ? ???00? > ? ? ? ???00 > GG? ? ? ? ???GG? > ? ? ? ???00 > 00? ? ? ? ???AA? > ? ? ? ???00 > > I want to read the file into R. > 1) when I use 'read.table' without 'header=T' option, > > temp <- read.table('temp.txt') > # I got > > temp > ? ? V1???V2???V3 > 1 snp1 snp2 snp3 > 2???AA???00???00 > 3???GG???GG???00 > 4???00???AA???00 > > 2) If I include the 'header=T' option, > > temp <- read.table('temp.txt', header=T, as.is=T) > # I got > > temp > ? snp1 snp2 snp3 > 1???AA???00? ? 0 > 2???GG???GG? ? 0 > 3???00???AA? ? 0 > > The difference is for 'snp3', we can see, in 1) the values > for snp3 are all > '00', while in 2) the values are all '0'.? > ???How can I keep the original > values for snp3 as '00', meanwhile get the 'headers' or say > the colnames as > 'snp1? ? ? ? snp2? ? ? > ? snp3'? > > thank you very much, > > karena > > -- > View this message in context: http://r.789695.n4.nabble.com/a-question-about-read-table-with-or-without-read-table-urgent-tp2314423p2314423.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >