简体   繁体   English

选择填充R中特定条件的数据框的行

[英]Selecting rows of a dataframe fullfilling an specific condition in R

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. 我必须选择满足一个特定条件ex的行。 which(DT_$var>value, arr.ind = T)) or which(DT_$var>value && DT_$var2>value2, arr.ind = T)) 哪个(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 我希望你能帮助我

There is a very good chance that you want to use dplyr and it's filter function. 您很有可能要使用dplyr及其filter功能。 It would work like this: 它将像这样工作:

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

You don't need to use DT$var and DT$var2 here; 您无需在此处使用DT$varDT$var2 dplyr knows what you mean when you refer to variables. 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只是受虐狂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM