Re: [R] read.table with numeric row names

2012-07-15 Thread arun
Hi Peter,

I copied the data from your email and run it again.

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) 

dat1
    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


colnames(dat1)-gsub(^[X](.*),\\1,colnames(dat1))


I am  not sure what happened with your end.  May be you could try
readtable(, fill=TRUE)

I guess Chi was able to read it as I understood from his email: 
(Thanks. It works very good.Chi

A.K.




- Original Message -
From: peter dalgaard pda...@gmail.com
To: arun smartpink...@yahoo.com
Cc: kexinz zhangchic...@gmail.com; R help r-help@r-project.org
Sent: Friday, July 13, 2012 10:27 AM
Subject: Re: [R] read.table with numeric row names


On Jul 13, 2012, at 04:27 , arun wrote:

 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)

(This didn't survive too well in mail:

 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)  
Error in read.table(text =  \n 2.5  3.6  7.1  7.9 \n 100  3      4      2    3 
\n 200  3.1  4      3      3 \n 300  2.2  3.3  2    4 \n ,  : 
  more columns than column names

Not sure exactly what happened there...)



  
 #The code should remove the X from the column names (row names?)
 

However, adding check.names=FALSE should be more expedient.

 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 zhangchic...@gmail.com
 To: r-help@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@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.
 
 
 __
 R-help@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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-13 Thread peter dalgaard

On Jul 13, 2012, at 04:27 , arun wrote:

 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  23 
  200  3.1  4  3  3 
  300  2.2  3.3  24 
  ,sep=,header=TRUE)

(This didn't survive too well in mail:

 dat1-read.table(text= 
+  2.5  3.6  7.1  7.9 
+  100  3  4  23 
+  200  3.1  4  3  3 
+  300  2.2  3.3  24 
+  ,sep=,header=TRUE)  
Error in read.table(text =  \n 2.5  3.6  7.1  7.9 \n 100  3  4  23 
\n 200  3.1  4  3  3 \n 300  2.2  3.3  24 \n ,  : 
  more columns than column names

Not sure exactly what happened there...)

 

  
 #The code should remove the X from the column names (row names?)
 

However, adding check.names=FALSE should be more expedient.

 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 zhangchic...@gmail.com
 To: r-help@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@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.
 
 
 __
 R-help@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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-13 Thread jim holtman
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)

 x
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
 names(x)
[1] 2.5 3.6 7.1 7.9


On Thu, Jul 12, 2012 at 2:50 PM, kexinz zhangchic...@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@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.

__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-13 Thread William Dunlap
 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  23 
  +  200  3.1  4  3  3 
  +  300  2.2  3.3  24
  + ) # 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.023
  200  3.1  4.033
  300  2.2  3.324
   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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
 Behalf Of kexinz
 Sent: Thursday, July 12, 2012 2:59 PM
 To: r-help@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@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.

__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-13 Thread kexinz
Thanks all you guys' help!

--
View this message in context: 
http://r.789695.n4.nabble.com/read-table-with-numeric-row-names-tp4636342p4636446.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-13 Thread peter dalgaard

On Jul 13, 2012, at 17:59 , arun wrote:

 Hi Peter,
 
 I copied the data from your email and run it again.
 
 dat1-read.table(text=
   2.5  3.6  7.1  7.9
   100  3  4  23
   200  3.1  4  3  3
   300  2.2  3.3  24
   ,sep=,header=TRUE) 
 
 dat1
 X2.5 X3.6 X7.1 X7.9
 100  3.0  4.023
 200  3.1  4.033
 300  2.2  3.324
 
 
 colnames(dat1)-gsub(^[X](.*),\\1,colnames(dat1))
 
 
 I am  not sure what happened with your end.  May be you could try
 readtable(, fill=TRUE)
 

More likely, I need something to filter out oddball characters inserted by 
Nabble or one of the mail agents. Watch this: Cut+paste from arun (22:17), Chi 
(07:09), and arun (07:08)

 dat1-read.table(text=
+  2.5  3.6  7.1  7.9
+  100  3  4  23
+  200  3.1  4  3  3
+  300  2.2  3.3  24
+  ,sep=,header=TRUE)
 dat1-read.table(text=
+ 2.5  3.6  7.1  7.9
+ 100  3  4  23
+ 200  3.1  4  3  3
+ 300  2.2  3.3  24
+ ,sep=,header=TRUE)
 dat1-read.table(text= 
+  2.5  3.6  7.1  7.9 
+  100  3  4  23 
+  200  3.1  4  3  3 
+  300  2.2  3.3  24 
+  ,sep=,header=TRUE)  
Error in read.table(text =  \n 2.5  3.6  7.1  7.9 \n 100  3  4  23 
\n 200  3.1  4  3  3 \n 300  2.2  3.3  24 \n ,  : 
  more columns than column names

The clue seems to be that the 3rd variant has a trailing space added to each 
line. No big deal, just drove me up the wall for while this afternoon...


 I guess Chi was able to read it as I understood from his email: 
 (Thanks. It works very good.Chi
 
 A.K.
 
 
 
 
 - Original Message -
 From: peter dalgaard pda...@gmail.com
 To: arun smartpink...@yahoo.com
 Cc: kexinz zhangchic...@gmail.com; R help r-help@r-project.org
 Sent: Friday, July 13, 2012 10:27 AM
 Subject: Re: [R] read.table with numeric row names
 
 
 On Jul 13, 2012, at 04:27 , arun wrote:
 
 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  23 
   200  3.1  4  3  3 
   300  2.2  3.3  24 
   ,sep=,header=TRUE)
 
 (This didn't survive too well in mail:
 
 dat1-read.table(text= 
 +  2.5  3.6  7.1  7.9 
 +  100  3  4  23 
 +  200  3.1  4  3  3 
 +  300  2.2  3.3  24 
 +  ,sep=,header=TRUE)  
 Error in read.table(text =  \n 2.5  3.6  7.1  7.9 \n 100  3  4  2
 3 \n 200  3.1  4  3  3 \n 300  2.2  3.3  24 \n ,  : 
   more columns than column names
 
 Not sure exactly what happened there...)
 
 
 
   
 #The code should remove the X from the column names (row names?)
 
 
 However, adding check.names=FALSE should be more expedient.
 
 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 zhangchic...@gmail.com
 To: r-help@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@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.
 
 
 __
 R-help@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.
 
 -- 
 Peter Dalgaard, Professor
 Center for Statistics, Copenhagen Business School
 Solbjerg Plads 3, 2000 Frederiksberg, Denmark
 Phone: (+45)38153501
 Email: pd@cbs.dk  Priv: pda...@gmail.com

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R

[R] read.table with numeric row names

2012-07-12 Thread kexinz
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@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.


Re: [R] read.table with numeric row names

2012-07-12 Thread arun
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 zhangchic...@gmail.com
To: r-help@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@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.


__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-12 Thread Yasir Kaheil
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.

__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-12 Thread kexinz
Thanks, but I don't want to specify the column names by hand, since I have a
lot of similar files.


arun kirshna wrote
 
 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.
 
 


--
View this message in context: 
http://r.789695.n4.nabble.com/read-table-with-numeric-row-names-tp4636342p4636378.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-12 Thread kexinz
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@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.


Re: [R] read.table with numeric row names

2012-07-12 Thread arun
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 zhangchic...@gmail.com
To: r-help@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@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.


__
R-help@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.


Re: [R] read.table with numeric row names

2012-07-12 Thread Chi Zhang
Thanks. It works very good.
Chi

On Thu, Jul 12, 2012 at 7:27 PM, arun smartpink...@yahoo.com wrote:

 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  23
  200  3.1  4  3  3
  300  2.2  3.3  24
  ,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 zhangchic...@gmail.com
 To: r-help@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.




[[alternative HTML version deleted]]

__
R-help@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.