[英]R: using dplyr to remove certain rows in the data.frame
dat <- data.frame(ID = c(1, 2, 2, 2), Gender = c("Both", "Both", "Male", "Female"))
> dat
ID Gender
1 1 Both
2 2 Both
3 2 Male
4 2 Female
對於每一個ID,如果性別是Both
, Male
和Female
,我想與刪除行Both
。 也就是說,我想要的數據是這樣的:
ID Gender
1 1 Both
2 2 Male
3 2 Female
我嘗試通過使用下面的代碼來做到這一點:
library(dplyr)
> dat %>%
group_by(ID) %>%
mutate(A = ifelse(length(unique(Gender)) >= 3 & Gender == 'Both', F, T)) %>%
filter(A) %>%
select(-A)
# A tibble: 2 x 2
# Groups: ID [1]
ID Gender
<dbl> <fctr>
1 2 Male
2 2 Female
我聲明了一個虛擬變量稱為A
,其中A = F
,如果對於給定的ID
,在所有3個元素Gender
存在(“兩者”,“男性”和“女性”,這些都是不同的值即Gender
可以采取,則不能再使用其他值),並且相應的行具有Gender == Both
。 然后,我將刪除該行。
但是,即使我的Gender
僅是“兩個”,但不是“兩個”,“男性”和“女性”,似乎我還是在第一行中分配了A = F
?
按“ ID”分組后,創建一個邏輯條件,其中“性別”不是“兩個”,並且“性別”中distinct
元素的長度為3,即“男性”,“女性”,“兩個”(如操作說明所述) (沒有其他值)或( |
)如果元素數僅為1
dat %>%
group_by(ID) %>%
filter((Gender != "Both" & n_distinct(Gender)==3)| n() ==1 )
# A tibble: 3 x 2
# Groups: ID [2]
# ID Gender
# <dbl> <fct>
#1 1 Both
#2 2 Male
#3 2 Female
或另一個選擇是
dat %>%
group_by(ID) %>%
filter(Gender %in% c("Male", "Female")| n() == 1)
# A tibble: 3 x 2
# Groups: ID [2]
# ID Gender
# <dbl> <fct>
#1 1 Both
#2 2 Male
#3 2 Female
從底數R開始,使用ave
dat[!(ave(dat$Gender,dat$ID,FUN=function(x) length(unique(x)))!='1'&(dat$Gender=='Both')),]
ID Gender
1 1 Both
3 2 Male
4 2 Female
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.