[R] how to subset the data frame by lines

R. Michael Weylandt michael.weylandt at gmail.com
Thu Aug 2 00:06:51 CEST 2012


Specifically note that, unlike C, R has true multidimensional arrays
which are more than pointer jumps: the practical result of this is
that you want to use a single subsetting ( x[i,j] ) and specify them
both in one call  -- x[i][j] instead gives you the "j"th element of
the "i"th element which may or may not exist, but likely isn't what
you are looking for.

On Wed, Aug 1, 2012 at 4:27 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> Please read An Introduction to R, which ships with every copy of R,
> where you will learn how to properly subscript data frames and lists,
> among other things. There is a reason for such tutorials -- they
> enable you to avoid elementary mistakes like this and wasting time and
> effort with such posts as this.
>
> -- Bert
>
> On Wed, Aug 1, 2012 at 2:13 PM, Jie Tang <totangjie at gmail.com> wrote:
>> I defined a data.frame by a two-dimensional array.
>>> aa = data.frame(rbind(  11:20,   1:10))
>>> aa
>>   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
>> 1 11 12 13 14 15 16 17 18 19  20
>> 2  1   2   3     4    5  6   7    8  9  10
>>
>> Now I want to use the data in the second line of aa which from 1 to 10 to
>>
>> output.But I find that when I use the aa[[1]][1:10] and it seems be mistake.
>> aa[[1]][1:10]
>>  [1] 11  1 NA NA NA NA NA NA NA NA

You really just want this:

aa[2, ]

Second row -- not specifying the column returns all of them. And note
that we are 1-based in our indexing here!

Michael

>>
>> But when I just indice one element of the dataframe .e.g.aa[[1]][2]
>> It will be ok. How could I subset the data.frame data by lines?
>>
>> Thank you ?
>>
>> --
>> TANG Jie
>> Email: totangjie at gmail.com
>> Tel: 0086-2154896104
>> Shanghai Typhoon Institute,China
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
> ______________________________________________
> 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