选择满足R中特定条件的数据帧行

时间:2022-01-01 11:14:58

First of all, I have to say that this is my first post. Despite of having look for the answer using the search toolbox it might be possible that I passed over the right topic without realizing myself, so just in case sorry for that.

首先,我不得不说这是我的第一篇文章。尽管使用搜索工具箱寻找答案,但我可能会在没有意识到自己的情况下通过正确的主题,所以为了对不起。

Having said that, my problem is the following one:

话虽如此,我的问题是以下问题:

  • I have a data table composed by several columns.
  • 我有一个由几列组成的数据表。

  • I have to select the rows that are fullfilling one specific condition ex. which(DT_$var>value, arr.ind = T)) or which(DT_$var>value && DT_$var2>value2, arr.ind = T))
  • 我必须选择满足一个特定条件ex的行。其中(DT_ $ var> value,arr.ind = T))或其中(DT_ $ var> value && DT_ $ var2> value2,arr.ind = T))

  • I have to keep these columns in a new data frame.
  • 我必须将这些列保留在新的数据框中。

My approach was the following one but it is not working, probably because I did not understand the loops correctly:

我的方法是以下一种,但它不起作用,可能是因为我没有正确理解循环:

while (i in nrow(DT)) {
    if(DT$var[i]>value){
        DT_aux[i]=DT[i]
        i<-i+1
    }

}
Error in if (DT$value[i] > 45) { : argument is of length zero

I hope that you can help me

我希望你能帮助我

1 个解决方案

#1


2  

There is a very good chance that you want to use dplyr and it's filter function. It would work like this:

您很有可能想要使用dplyr及其过滤功能。它会像这样工作:

library(dplyr)
DT %>% filter(var>value && var2>value2)

You don't need to use DT$var and DT$var2 here; dplyr knows what you mean when you refer to variables.

你不需要在这里使用DT $ var和DT $ var2;当你引用变量时,dplyr知道你的意思。

You can, of course, do the same with base R, but this kind of work is exactly what dplyr was made for, so sticking with base R, in this case, is just masochism.

当然,你可以用基础R做同样的事情,但是这种工作正是dplyr所做的,所以在这种情况下,坚持基础R只是受虐狂。

#1


2  

There is a very good chance that you want to use dplyr and it's filter function. It would work like this:

您很有可能想要使用dplyr及其过滤功能。它会像这样工作:

library(dplyr)
DT %>% filter(var>value && var2>value2)

You don't need to use DT$var and DT$var2 here; dplyr knows what you mean when you refer to variables.

你不需要在这里使用DT $ var和DT $ var2;当你引用变量时,dplyr知道你的意思。

You can, of course, do the same with base R, but this kind of work is exactly what dplyr was made for, so sticking with base R, in this case, is just masochism.

当然,你可以用基础R做同样的事情,但是这种工作正是dplyr所做的,所以在这种情况下,坚持基础R只是受虐狂。