所以我有一個如下所示的資料框,我想洗掉所有列 a、b 或 c 的行,其中包含的值等于幾個數字之一。
即在下面,如果我想洗掉條目等于 1 的行,我只想要第二行(我想忽略前幾列)。
| 沒有什么 | 一個 | 乙 | C |
|---|---|---|---|
| 1 | 1 | 2 | 8 |
| 1 | 2 | 3 | 2 |
| 1 | 4 | 6 | 1 |
回報:
| 沒有什么 | 一個 | 乙 | C |
|---|---|---|---|
| 1 | 2 | 3 | 2 |
應該注意,在我想忽略的前幾列之后有很多列,所以我不想單獨命名它們。
uj5u.com熱心網友回復:
使用 base-r 的解決方案。
## identify which rows in the df contain 1s
rows_to_remove = which(df[,-1] == 1, arr.ind=T)[,1]
# subset these rows
df[-rows_to_remove,]
nothing a b c
2 1 2 3 2
uj5u.com熱心網友回復:
您可以使用dplyr::if_any.
df %>%
filter(!if_any(c(a,b,c), ~ .x == 1))
nothing a b c
1 1 2 3 2
您也可以選擇-或者:如果列是連續的:
filter(df, !if_any(-nothing, ~ .x == 1))
filter(df, !if_any(a:c, ~ .x == 1))
資料
df <- structure(list(nothing = c(1L, 1L, 1L), a = c(1L, 2L, 4L), b = c(2L,
3L, 6L), c = c(8L, 2L, 1L)), class = "data.frame", row.names = c(NA, -3L))
uj5u.com熱心網友回復:
在這種情況下,我們可以使用filterwithacross和不帶if_any.
library(dplyr)
filter(df, across(a:c, ~. != 1))
nothing a b c
1 1 2 3 2
uj5u.com熱心網友回復:
試試這個
df[rowSums(df[-1]==1)==0,]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/404932.html
標籤:
上一篇:有條件地跨列同步異步資料
