Re: [R] Is this an artifact of using "which"?

From: Tania Oh <tania.oh_at_bnc.ox.ac.uk>
Date: Mon, 14 Apr 2008 12:55:46 +0100

Dear Uwe,
thank you very much for this.
After reading your solution below, I searched the help pages for data.frame, which, factor but I didn't see the option for "drop" in them.

I googled and found "drop" associated with the function subset. is this the help page you were alluding to?

Sorry if I've missed something.
thanks so much in advance again.
tania

On 14 Apr 2008, at 12:39, Uwe Ligges wrote:

>
>
> Tania Oh wrote:
>> Dear all,
>> I used "which" to obtain a subset of values from my data.frame.
>> however, I find that there is a "trace" of the values I have
>> removed. Any suggestions would be greatly appreciate.
>> Below is my data:
>> d <- data.frame( val = 1:10,
>> group = sample(LETTERS[1:5], 10, repl=TRUE) )
>> >d
>> val group
>> 1 1 B
>> 2 2 E
>> 3 3 B
>> 4 4 C
>> 5 5 A
>> 6 6 B
>> 7 7 A
>> 8 8 E
>> 9 9 E
>> 10 10 A
>> ## selecting everything that is not group "A"
>> d<-d[which(d$group !="A"),]
>> > d
>> val group
>> 1 1 B
>> 2 2 E
>> 3 3 B
>> 4 4 C
>> 6 6 B
>> 8 8 E
>> 9 9 E
>> > levels(d$group)
>> [1] "A" "B" "C" "E"
>> ## why is group A still reflected here?
>
>
> Because you have removed elements from a factor objects that has
> particular levels. You remove elements (=observations), but the
> factor still knows that all levels are possible (stired in
> attributes of the object).
>
> If you want to remove all levels without corresponding observations,
> use explicit drop=TRUE as the help page suggests, e.g.:
>
>
> d <- d[d$group != "A", ]
> d$group <- d$group[ , drop = TRUE]
>
> Uwe Ligges
>
>
>
>> Many thanks in advance,
>> tania
>> D.phil student
>> Department of Physiology, Anatomy and Genetics
>> Oxford University
>> ______________________________________________
>> 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.



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. Received on Mon 14 Apr 2008 - 11:58:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Apr 2008 - 16:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive