I have a text file like this 2.5 3.6 7.1 7.9 100 3 4 2 3 200 3.1 4 3 3 300 2.2 3.3 2 4 I used "r <- read.table("a.txt", header=T)" The row names becomes X2.5, X3.6... What I need is the row names are numeric, so I can use the row names as numbers on x-axis for plotting. e.g. "plot(colMeans(r)~names(r))", something like this. How to do this? Thanks. -- View this message in context: http://r.789695.n4.nabble.com/read-table-with-numeric-row-names-tp4636342.html Sent from the R help mailing list archive at Nabble.com.
Hi, Try this: dat1<-read.table(text=" ?2.5? 3.6? 7.1? 7.9 ?100? 3????? 4????? 2??? 3 ?200? 3.1? 4????? 3????? 3 ?300? 2.2? 3.3? 2??? 4 ?",sep="",header=TRUE) #Either colnames(dat1)<-c("2.5","3.6","7.1","7.9") #or colnames(dat1)<-c(2.5,3.6,7.1,7.9) #produce character column names ?is.character(colnames(dat1)) [1] TRUE ?is.numeric(colnames(dat1)) [1] FALSE dat1 ??? 2.5 3.6 7.1 7.9 100 3.0 4.0?? 2?? 3 200 3.1 4.0?? 3?? 3 300 2.2 3.3?? 2?? 4 A.K. ----- Original Message ----- From: kexinz <zhangchicool at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, July 12, 2012 2:50 PM Subject: [R] read.table with numeric row names I have a text file like this ? ? ? ? 2.5? 3.6? 7.1? 7.9 100? 3? ? ? 4? ? ? 2? ? 3 200? 3.1? 4? ? ? 3? ? ? 3 300? 2.2? 3.3? 2? ? 4 I used "r <- read.table("a.txt", header=T)" The row names becomes X2.5, X3.6... What I need is the row names are numeric, so I can use the row names as numbers on x-axis for plotting. e.g. "plot(colMeans(r)~names(r))", something like this. How to do this? Thanks. -- View this message in context: http://r.789695.n4.nabble.com/read-table-with-numeric-row-names-tp4636342.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.
just do this: colnames(r)<-substr(colnames(r),2,nchar(colnames(r))) This will remove the X. Later when you want to use the headed to plot something, cast it as numeric: plot(colMeans(r)~as.numeric(colnames(r))) ----- Yasir Kaheil -- View this message in context: http://r.789695.n4.nabble.com/read-table-with-numeric-row-names-tp4636342p4636354.html Sent from the R help mailing list archive at Nabble.com.
Thanks Yasir, this helps a lot. BTW, is there an R command to read just the first line of the file? Yasir Kaheil wrote> > just do this: > colnames(r)<-substr(colnames(r),2,nchar(colnames(r))) > > This will remove the X. > Later when you want to use the headed to plot something, cast it as > numeric: > plot(colMeans(r)~as.numeric(colnames(r))) >-- View this message in context: http://r.789695.n4.nabble.com/read-table-with-numeric-row-names-tp4636342p4636377.html Sent from the R help mailing list archive at Nabble.com.
Hello, I saw your reply in nabble.? Sorry about that.? I thought the dataset had only few columns. #You can read first line of a file using: readLines("foo.txt",n=1)[1] #The more generic colname substitution dat1<-read.table(text=" ?2.5? 3.6? 7.1? 7.9 ?100? 3????? 4????? 2??? 3 ?200? 3.1? 4????? 3????? 3 ?300? 2.2? 3.3? 2??? 4 ?",sep="",header=TRUE)? #The code should remove the "X" from the column names (row names?) colnames(dat1)<-gsub("^[X](.*)","\\1",colnames(dat1)) dat1 ??? 2.5 3.6 7.1 7.9 100 3.0 4.0?? 2?? 3 200 3.1 4.0?? 3?? 3 300 2.2 3.3?? 2?? 4 plot(colMeans(dat1)~as.numeric(names(dat1)),xlab="Column_Name",ylab="Column_Mean") A.K. ----- Original Message ----- From: kexinz <zhangchicool at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, July 12, 2012 2:50 PM Subject: [R] read.table with numeric row names I have a text file like this ? ? ? ? 2.5? 3.6? 7.1? 7.9 100? 3? ? ? 4? ? ? 2? ? 3 200? 3.1? 4? ? ? 3? ? ? 3 300? 2.2? 3.3? 2? ? 4 I used "r <- read.table("a.txt", header=T)" The row names becomes X2.5, X3.6... What I need is the row names are numeric, so I can use the row names as numbers on x-axis for plotting. e.g. "plot(colMeans(r)~names(r))", something like this. How to do this? Thanks. -- View this message in context: http://r.789695.n4.nabble.com/read-table-with-numeric-row-names-tp4636342.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.
Try this:> x <- read.table(text = " 2.5 3.6 7.1 7.9+ 100 3 4 2 3 + 200 3.1 4 3 3 + 300 2.2 3.3 2 4", header = TRUE, check.names = FALSE)> > x2.5 3.6 7.1 7.9 100 3.0 4.0 2 3 200 3.1 4.0 3 3 300 2.2 3.3 2 4> names(x)[1] "2.5" "3.6" "7.1" "7.9" On Thu, Jul 12, 2012 at 2:50 PM, kexinz <zhangchicool at gmail.com> wrote:> I have a text file like this > 2.5 3.6 7.1 7.9 > 100 3 4 2 3 > 200 3.1 4 3 3 > 300 2.2 3.3 2 4 > > I used "r <- read.table("a.txt", header=T)" > The row names becomes X2.5, X3.6... What I need is the row names are > numeric, so I can use the row names as numbers on x-axis for plotting. e.g. > "plot(colMeans(r)~names(r))", something like this. How to do this? > > Thanks. > > -- > View this message in context: http://r.789695.n4.nabble.com/read-table-with-numeric-row-names-tp4636342.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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.