I am trying to add new column named 'class' to my data based on the conditions of the columns. I built user defined function called class_fun to create this new column.
I try to set entire sub-group to same value if the condition is matched. But I have not succeeded yet!
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
here is the function for creating a new column names class
class_fun <- function(set,vals){
if(any(grepl("a|b",set)&set==vals)){
"catched"
}
else{
NA
}
}
library(dplyr)
test%>%
group_by(set,gr)%>%
mutate(class=class_fun(set,vals))
which gives
# 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
What I am expecting was
# 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
any idea or solution why this is happening ? thx!
Aucun commentaire:
Enregistrer un commentaire