[R] accessing a data frame with row names

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 31 13:38:47 CEST 2010


Use read.csv or read.table(..., sep = ",").  Also note that if you
delete the first comma of the header (as in the second example below)
you won't have to specify row.names since it can figure it out from
the fact that there is one fewer column name than data fields.

> Lines <- ",column1,column2
+ row1,0.1,0.2
+ row2,0.3,0.4"
>
> read.csv(textConnection(Lines), row.names = 1)
     column1 column2
row1     0.1     0.2
row2     0.3     0.4
>
>
> Lines2 <- "column1,column2
+ row1,0.1,0.2
+ row2,0.3,0.4"
>
> read.csv(textConnection(Lines2))
     column1 column2
row1     0.1     0.2
row2     0.3     0.4


On Mon, May 31, 2010 at 7:23 AM, e-letter <inpost at gmail.com> wrote:
> Readers,
>
> I have entered a file into r:
>
> ,column1,column2
> row1,0.1,0.2
> row2,0.3,0.4
>
> using the command:
>
> dataframe<-read.table("/path/to/file.csv",header=T,row.names=1)
>
> When I try the command:
>
> dataframe[,2]
>
> I receive the response:
>
> NULL
>
> I was expecting:
>
> row1 0.2
> row2 0.4
>
> What is my error with the syntax please?
>
> Yours,
>
> r251
> mandriva2009
>
> ______________________________________________
> 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