【问题标题】:Conditional filtering with data.table with multiple statements使用带有多个语句的 data.table 进行条件过滤
【发布时间】:2022-01-09 03:25:59
【问题描述】:

我想知道是否有一种优雅简洁的方法来使用 data.table 进行条件过滤。

我的目标是: 如果满足条件1,则根据条件2进行过滤。

例如,在 iris 数据集的情况下, 如何在Sepal.Length<5.5 中删除Species=="setosa" 中的观察结果,同时将所有观察结果保留在Sepal.Length<5.5 中以用于其他物种?

我知道如何分步执行此操作,但我想知道是否有更好的方法在单个班轮中执行此操作

# this is how I would do it in steps. 

data("iris")

# first only select observations in setosa I am interested in keeping 
iris1<- setDT(iris)[Sepal.Length>=5.5&Species=="setosa"] 

# second, drop all of setosa observations. 
iris2<- setDT(iris)[Species!="setosa"] 

# join data,
iris_final<-full_join(iris1,iris2)

head(iris_final)
   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1:          5.8         4.0          1.2         0.2     setosa
2:          5.7         4.4          1.5         0.4     setosa
3:          5.7         3.8          1.7         0.3     setosa
4:          5.5         4.2          1.4         0.2     setosa
5:          5.5         3.5          1.3         0.2     setosa # only keeping setosa with Sepal.Length>=5.5. Note that for other species, Sepal.Length can be <5.5
6:          7.0         3.2          4.7         1.4 versicolor

有没有更简洁优雅的方式来做到这一点?

【问题讨论】:

    标签: r data.table subset


    【解决方案1】:

    您正在寻找类似以下的内容吗?不是很清楚你想要什么。

    library(data.table)
    
    dt <- data.table(iris)
    dt[Sepal.Length >= 5.5 & Species == "setosa" | Species != "setosa"]
    
    #>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
    #>   1:          5.8         4.0          1.2         0.2    setosa
    #>   2:          5.7         4.4          1.5         0.4    setosa
    #>   3:          5.7         3.8          1.7         0.3    setosa
    #>   4:          5.5         4.2          1.4         0.2    setosa
    #>   5:          5.5         3.5          1.3         0.2    setosa
    #>  ---                                                            
    #> 101:          6.7         3.0          5.2         2.3 virginica
    #> 102:          6.3         2.5          5.0         1.9 virginica
    #> 103:          6.5         3.0          5.2         2.0 virginica
    #> 104:          6.2         3.4          5.4         2.3 virginica
    #> 105:          5.9         3.0          5.1         1.8 virginica
    

    【讨论】:

      【解决方案2】:

      您可以使用| 或运算符:

      这是要求删除所有 Species=="setosa" &amp; Sepal.Length&lt;5.5 所在的行并保留 Sepal.Length&gt;5.5 所在的行

      iris1[!(Species=="setosa" & Sepal.Length<5.5) | Sepal.Length>5.5]
      
           Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
        1:          5.8         4.0          1.2         0.2    setosa
        2:          5.7         4.4          1.5         0.4    setosa
        3:          5.7         3.8          1.7         0.3    setosa
        4:          5.5         4.2          1.4         0.2    setosa
        5:          5.5         3.5          1.3         0.2    setosa
       ---                                                            
      101:          6.7         3.0          5.2         2.3 virginica
      102:          6.3         2.5          5.0         1.9 virginica
      103:          6.5         3.0          5.2         2.0 virginica
      104:          6.2         3.4          5.4         2.3 virginica
      105:          5.9         3.0          5.1         1.8 virginica
      

      【讨论】:

        猜你喜欢
        • 2021-11-04
        • 2021-10-08
        • 2021-11-25
        • 1970-01-01
        • 1970-01-01
        • 2023-02-08
        • 1970-01-01
        • 1970-01-01
        • 2019-12-23
        相关资源
        最近更新 更多