![](/img/trans.png)
[英]Filtering rows with group by and matching different strings in multiple columns
[英]Searching matching rows in different columns
我正在尋找一個可以在列之間找到匹配項並輸出的函數,如果找到匹配的行則輸出"has matches"
否則為"no matches"
例如
df = data.frame(id=c("good","bad","ugly","dirty","clean","frenzy"),di=c(1,2,"good","dirty",4,"ugly"))
> df
id di
1 good 1
2 bad 2
3 ugly good
4 dirty dirty
5 clean 4
6 frenzy ugly
我想檢查di
列是否與id
列匹配,從而
> df
id di match
1 good 1 no matches
2 bad 2 no matches
3 ugly good has matches
4 dirty dirty has matches
5 clean 4 no matches
6 frenzy ugly has matches
我正在尋找的這種方法
match_func <- function(x,y){
}
df%>%
do(match_func(.$id,.$di))
提前致謝!
使用base R
且不使用if/else
語句,可以使用以下命令計算match
列:
df$match <- c("no matches", "has matches")[(df$di %in% df$id) + 1]
df
# id di match
#1 good 1 no matches
#2 bad 2 no matches
#3 ugly good has matches
#4 dirty dirty has matches
#5 clean 4 no matches
#6 frenzy ugly has matches
只需將%in%
與ifelse
一起ifelse
df %>%
mutate(match = ifelse(di %in% id, "has matches", "no matches"))
或case_when
df %>%
mutate(match = case_when(di %in% id ~ "has matches",
TRUE ~ "no matches"))
這可以直接包裝在函數中。 假設我們傳遞的是未加引號的名稱,然后將其轉換為具有enquo
,然后在mutate
求值!!
f1 <- function(dat, col1, col2) {
col1 = enquo(col1)
col2 = enquo(col2)
dat %>%
mutate(match = case_when(!! (col1) %in% !!(col2) ~ "has matches",
TRUE ~ "no matches"))
}
f1(df, di, id)
# id di match
#1 good 1 no matches
#2 bad 2 no matches
#3 ugly good has matches
#4 dirty dirty has matches
#5 clean 4 no matches
#6 frenzy ugly has matches
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.