[R] read.table with numeric row names
William Dunlap
wdunlap at tibco.com
Fri Jul 13 17:32:18 CEST 2012
> BTW, is there an R command to read just the first line of the file?
scan() or readLines() will read as many lines of the file as you want.
Use the file() function to open a "file connection" so a subsequent
read.table() will start where scan() or readLines() finished. E.g.,
> tfile <- tempfile()
> cat(file=tfile, " 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
+ ") # now tfile looks like your example file
> read.table(header=TRUE, tfile) # easy way, but not what you want
X2.5 X3.6 X7.1 X7.9
100 3.0 4.0 2 3
200 3.1 4.0 3 3
300 2.2 3.3 2 4
> fileConn <- file(tfile, open="r")
> scan(fileConn, what=0.0, nlines=1)
Read 4 items
[1] 2.5 3.6 7.1 7.9
> read.table(header=FALSE, fileConn)
V1 V2 V3 V4 V5
1 100 3.0 4.0 2 3
2 200 3.1 4.0 3 3
3 300 2.2 3.3 2 4
> str(.Last.value)
'data.frame': 3 obs. of 5 variables:
$ V1: int 100 200 300
$ V2: num 3 3.1 2.2
$ V3: num 4 4 3.3
$ V4: int 2 3 2
$ V5: int 3 3 4
> close(fileConn) # clean up
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of kexinz
> Sent: Thursday, July 12, 2012 2:59 PM
> To: r-help at r-project.org
> Subject: Re: [R] read.table with numeric row names
>
> 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list