accessing a data frame with row names

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

accessing a data frame with row names

e-letter
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: accessing a data frame with row names

Gabor Grothendieck
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: accessing a data frame with row names

Ivan Calandra
In reply to this post by e-letter
Hi,

Let's create your data.frame:
 > dataframe <- structure(list(column1 = c(0.1, 0.3), column2 = c(0.2,
0.4)), .Names = c("column1",
"column2"), row.names = c("row1", "row2"), class = "data.frame")
 > dataframe[,2]
[1] 0.2 0.4
 > dataframe[,2, drop=FALSE]
      column2
row1     0.2
row2     0.4

So I don't know what's wrong with your data.
Maybe, providing the output from str(dataframe) would help us to help you!

HTH,
Ivan

Le 5/31/2010 13:23, e-letter a écrit :

> 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
>
> ______________________________________________
> [hidden email] 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.
>
>    

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: accessing a data frame with row names

e-letter
In reply to this post by Gabor Grothendieck
On 31/05/2010, Gabor Grothendieck <[hidden email]> wrote:

> 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

Thank you. When I enter the command:

max(dataframe[,2])

The response is:

[1] 0.4

But I want to receive the row name, i.e.:

[1] row2 0.4

Is this possible?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: accessing a data frame with row names

PIKAL Petr
Hi

[hidden email] napsal dne 01.06.2010 10:20:35:

> On 31/05/2010, Gabor Grothendieck <[hidden email]> wrote:
> > 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
>
> Thank you. When I enter the command:
>
> max(dataframe[,2])
>
> The response is:
>
> [1] 0.4
>
> But I want to receive the row name, i.e.:
>
> [1] row2 0.4

It seems that you probably shall consult

?which with parameter arr.ind=T

which(dataframe==max(dataframe), arr.ind=T)

Regards
Petr


>
> Is this possible?
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.