assume we have the following data frame
foo
k h=1 h=2 h=3
1 3 3 6 9
2 2 2 5 8
3 1 1 4 7
with
str(check)
'data.frame': 3 obs. of 4 variables:
$ k : Factor w/ 3 levels "3","2","1": 1 2 3
$ h=1: int 3 2 1
$ h=2: int 6 5 4
$ h=3: int 9 8 7
How can I subset my dataframe based on the factor of k
? For instance, to get only the row for k=3 or all rows k<3. I tried working with subet(foo, k=3)
but it doesn't work. I also tried to convert the column k to numeric, but then my data.frame loses its order. It's important that the data is of descending order with regard to k (so 3, 2, 1)
Bracket notation should be able to subset on factors without any problems:
# Returns all rows of foo where k == '3'
foo[foo$k == '3',]
Two possible problems with what you did before:
1) subset(foo, k=3)
should be subset(foo, k==3)
, don't confuse the equality operator ( ==
) with the assignment operator ( =
)
2) Since you're comparing with the actual level of your factor, you should check for equality with the character '3'
instead of the numeric 3
. You can see in the output from str()
that k's levels are "3","2","1"
, with quotes, whereas the integers for the other variables are shown without quotes 3 2 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.