簡體   English   中英

R 根據條件從 dataframe 中刪除所有具有特定 ID 的行

[英]R Remove all rows from dataframe with specific ID based on conditional

我在 R 中有一個 dataframe ,如下所示:

example <- matrix(c(1,1,2,3,3,50,90,85,80,100), ncol=2)
colnames(example) <- c('id', 'score')
example_df <- data.frame(example)


   id score
1:  1 50
2:  1 90
3:  2 10
4:  2 85
5:  3 80
6:  3 100

對於每個唯一 ID,如果該 ID 的分數在 20 到 60 之間,我想刪除該 ID 的所有行。在這種情況下,我生成的 dataframe 看起來像

   id score
1:  2 10
2:  2 85
3:  3 80
5:  3 100

因為 id 1 的得分為 50,介於 20 到 60 之間,所以我們刪除了 ID 為 1 的所有行。我該怎么做? 我曾考慮過使用group by但似乎 dplyr 動詞,如min maxmean在這種情況下無濟於事。

在基地 R 你可以這樣做:

 subset(example_df, ave(score>60|score<20, id, FUN = all))
   id score
3:  2    10
4:  2    85
5:  3    80
6:  3   100

甚至:

subset(example_df, !ave(score<60&score>20, id, FUN = any))
   id score
3:  2    10
4:  2    85
5:  3    80
6:  3   100

data.table的一個選項是將“data.frame”轉換為“data.table”( setDT ),按“id”分組,檢查 20 到 60 between是否沒有( !any “分數”,獲取行索引( .I ),提取為列( $V1 )並在i中使用它來子集行

library(data.table)
setDT(example_df)[example_df[, .I[!any(between(score, 20, 60))], by = id]$V1]

dplyr中的類似選項

library(dplyr)
example_df %>%
    group_by(id) %>%
    filter(!any(between(score, 20, 60))) %>%
    ungroup

-輸出

# A tibble: 4 x 2
#     id score
#  <int> <int>
#1     2    10
#2     2    85
#3     3    80
#4     3   100

數據

example_df <- structure(list(id = c(1L, 1L, 2L, 2L, 3L, 3L), score = c(50L, 
90L, 10L, 85L, 80L, 100L)), class = "data.frame", row.names = c("1:", 
"2:", "3:", "4:", "5:", "6:"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM