我试图根据列的条件向数据添加名为'class'
的新列。我构建了名为class_fun
的用户定义函数来创建此新列。
如果条件匹配,我会尝试将整个子组设置为相同的值。但是我还没有成功!
test=data.frame(set=as.numeric(gl(3,6)),gr = rep(letters[1:2],each=3),vals=c(c(10,10, 9, 8, 1,1),c(10,10,10, 9,6,2),c(10,7,6,1,1,2)))
> test
set gr vals
1 1 a 10
2 1 a 10
3 1 a 9
4 1 b 8
5 1 b 1
6 1 b 1
7 2 a 10
8 2 a 10
9 2 a 10
10 2 b 9
11 2 b 6
12 2 b 2
13 3 a 10
14 3 a 7
15 3 a 6
16 3 b 1
17 3 b 1
18 3 b 2
class
的新列的功能。class_fun <- function(gr,set,vals){
if(any(grepl("a|b",gr)&set==vals)){
"catched"
}
else{
NA
}
}
library(dplyr)
test%>%
group_by(set,gr)%>%
mutate(class=class_fun(gr,set,vals))
给出
# A tibble: 18 x 4
# Groups: set, gr [6]
set gr vals class
<dbl> <fct> <dbl> <lgl>
1 1 a 10 NA
2 1 a 10 NA
3 1 a 9 NA
4 1 b 8 NA
5 1 b 1 NA
6 1 b 1 NA
7 2 a 10 NA
8 2 a 10 NA
9 2 a 10 NA
10 2 b 9 NA
11 2 b 6 NA
12 2 b 2 NA
13 3 a 10 NA
14 3 a 7 NA
15 3 a 6 NA
16 3 b 1 NA
17 3 b 1 NA
18 3 b 2 NA
我期望的是
# A tibble: 18 x 4
# Groups: set, gr [6]
set gr vals class
<dbl> <fct> <dbl> <lgl>
1 1 a 10 NA
2 1 a 10 NA
3 1 a 9 NA
4 1 b 8 catched
5 1 b 1 catched
6 1 b 1 catched
7 2 a 10 NA
8 2 a 10 NA
9 2 a 10 NA
10 2 b 9 catched
11 2 b 6 catched
12 2 b 2 catched
13 3 a 10 NA
14 3 a 7 NA
15 3 a 6 NA
16 3 b 1 NA
17 3 b 1 NA
18 3 b 2 NA
为什么会发生这种情况的任何想法或解决方案? 谢谢!
答案 0 :(得分:1)
我认为不需要编写函数。
test%>%group_by(set,gr)%>%mutate(calss=ifelse(any(stringr::str_detect(gr,'a|b')&(set==vals)),'catched',NA)
+ )
# A tibble: 18 x 4
# Groups: set, gr [6]
set gr vals calss
<dbl> <fctr> <dbl> <chr>
1 1 a 10 <NA>
2 1 a 10 <NA>
3 1 a 9 <NA>
4 1 b 8 catched
5 1 b 1 catched
6 1 b 1 catched
7 2 a 10 <NA>
8 2 a 10 <NA>
9 2 a 10 <NA>
10 2 b 9 catched
11 2 b 6 catched
12 2 b 2 catched
13 3 a 10 <NA>
14 3 a 7 <NA>
15 3 a 6 <NA>
16 3 b 1 <NA>
17 3 b 1 <NA>
18 3 b 2 <NA>