[R] Stumped with subsetting
Peter Wilkinson
pwilkinson at videotron.ca
Thu Jul 29 22:03:26 CEST 2004
Hi there,
thanks for the reply. I was expecting what Tony posted. By the way I got
the format for my question out of Introduction to R. So I expected it to
work as in the text, but it turns out ....
my "list" somehow was not a vector:
> list[1]
[1] PERH0125
44 Levels: PERH0015 PERH0019 PERH0023 PERH0029 PERH0037 PERH0045 PERH0053
PERH0075 ... PERH0360
I did the following:
> list2 = as.vector(list)
> list2
[1] "PERH0125" "PERH0315" "PERH0161" "PERH0181" "PERH0229" "PERH0223"
"PERH0243" "PERH0269"
<snip>
I am not sure what format list was in (looks like factor), but I turned it
into a vector and it worked as expected. I have to go back and see how I
created list in the first place.
originally I took a row from a data frame, removed NA values, then sorted
it into an order that I wanted (using 'order') .... so I don't know when it
turned into something that looked like factor. Any ?
Peter
At 03:46 PM 7/29/2004, Sundar Dorai-Raj wrote:
>Sundar Dorai-Raj wrote:
>
>>
>>Peter Wilkinson wrote:
>>
>>>This seems like such a trivial thing to do:
>>>
>>>given a data.frame DF and variables w,v, x,y,z I can do
>>>
>>>DF["x"] or DF[c("x","y")]
>>>
>>>if I create a vector, mylist = c("x",y")
>>>
>>>then I do DF[mylist]
>>>
>>>I am not getting x and y, I get something else.
>>>
>>>
>>>what is the correct way to subset a data.frame by columns using a
>>>vector, as if I were doing DF["x","y"]?
>>>
>>>Peter
>>>
>>>______________________________________________
>>>R-help at stat.math.ethz.ch mailing list
>>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>>PLEASE do read the posting guide!
>>>http://www.R-project.org/posting-guide.html
>>
>>You want DF[, mylist]. DF[mylist] returns a list. You may want to look at
>>"An Introduction to R", which covers this topic.
>
>Sorry, typed too quickly.
>
>DF[mylist] and DF[, mylist] are the same if mylist is a vector with more
>than one element.
>
>After re-reading your post, I'm not quite sure what you are expecting.
>Sorry for any confusion....
>
>--sundar
>
More information about the R-help
mailing list