specifying conditions as a variable to subset a da

2020-07-20 03:49发布

Suppose I have a data frame, df with 30 columns: A1 to A30. I know that I can subset this data frame by writing a command like:

 filteredrows = subset(df, A1 == 30 & A2 == 2 & A3 == "this")

The above example filters data based on values in three columns, but I have to do this for values in about say 12 columns. Writing those 12 values in the subset() function will make it too long. To make the code cleaner, is there a way I can specify the condition as a variable or a function and then use that specify the conditions in the subset function. Is something like the following possible?

x = (A1 == 30 & A2 == 2 & A3 == "this")
filteredrows = subset(df, x)

Thanks in advance.

标签: r
2条回答
男人必须洒脱
2楼-- · 2020-07-20 04:14

You can specify the condition as an expression and then pass it to subset using eval:

d <- data.frame(x=letters[1:10],y=runif(10))
ss <- expression(x == "a")
subset(d, eval(ss))
查看更多
家丑人穷心不美
3楼-- · 2020-07-20 04:14

Your suggestion almost works, you just need a with when you get x.

> df <- expand.grid(A1=(1:3)*10,A2=1:3,A3=c("this","that"))
> x <- with(df, (A1 == 30 & A2 == 2 & A3 == "this"))
> subset(df, x)
  A1 A2   A3
6 30  2 this

You could also get the subset this way.

> df[x,]
  A1 A2   A3
6 30  2 this

You may also want to put x in the data frame df; otherwise reordering the data frame can mess things up, something like

df$x <- with(df, (A1 == 30 & A2 == 2 & A3 == "this"))
subset(df,x)
查看更多
登录 后发表回答