[R] Dataframe subset - why doesn't this work?

R. Michael Weylandt michael.weylandt at gmail.com
Fri Feb 17 15:48:11 CET 2012


Hi Ajay,

Like Jorge, I can't seem to reproduce the behavior you are worried about.

mtcars[rownames(mtcars) != "Valiant",]

returns a 31x11 data.frame as expected.

When you say it "fails," what error message / result are you seeing?

Michael


On Fri, Feb 17, 2012 at 3:27 AM, Jorge I Velez <jorgeivanvelez at gmail.com> wrote:
> Hi Ajay,
>
> In the first case, you need "==" instead of "=" :
>
> R> mtcars[ rownames(mtcars) == "Valiant", ]
>  mpg cyl disp  hp drat   wt  qsec vs am gear carb
> Valiant 18.1   6  225 105 2.76 3.46 20.22  1  0    3    1
>
> For the second case,
>
> R> mtcars[rownames(mtcars) != "Valiant",]
>
> will do it.
>
> See also ?subset.
>
> HTH,
> Jorge.-
>
>
> On Fri, Feb 17, 2012 at 3:02 AM, Ajay Askoolum <> wrote:
>
>> data(mtcars)
>>
>> mtcars[rownames(mtcars)!="Valiant",] # fails
>>
>> mtcars[list(rownames(mtcars))!="Valiant",] # runs but I am not getting the
>> expected result
>>
>> With the latter statement, I expected all rows except the one where the
>> name is "Valiant".
>>
>> I must have got something simple wrong; what is it?
>>
>> Thanks.
>>
>>        [[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.
>>
>
>        [[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.



More information about the R-help mailing list