I come to an issue that replacing a value if the condition is met. I use my own function data_manip that I can assign or add any other condition when needed.
However, when I try do use this function i changes all data withing that group. So far I could not find the similar post on here.
Here is what I tried,
df <- data.frame(percent = c(0.6, 0.7,1, 0.5,0.5,1,0.4,0.6,1),
type = rep(c("good", "bad","ugly"),each=3), smoke=rep(c('Visky','Wine','Wine'),3),
sex=rep(c('male','male','female'),3))
> df
percent type smoke sex
1 0.6 good Visky male
2 0.7 good Wine male
3 1.0 good Wine female
4 0.5 bad Visky male
5 0.5 bad Wine male
6 1.0 bad Wine female
7 0.4 ugly Visky male
8 0.6 ugly Wine male
9 1.0 ugly Wine female
data_manip <- function(x,gr){
if(grepl('goo|ug',gr)&&x<1){
x[x==0.6] <- 1
}
else
x
}
df%>%
group_by(type)%>%
mutate(percent_new=data_manip(percent,type))
gives
# A tibble: 9 x 5
# Groups: type [3]
percent type smoke sex percent_new
<dbl> <fctr> <fctr> <fctr> <dbl>
1 0.6 good Visky male 1.0
2 0.7 good Wine male 1.0
3 1.0 good Wine female 1.0
4 0.5 bad Visky male 0.5
5 0.5 bad Wine male 0.5
6 1.0 bad Wine female 1.0
7 0.4 ugly Visky male 1.0
8 0.6 ugly Wine male 1.0
9 1.0 ugly Wine female 1.0
I would like to keep original percent values if the condition is not for them.
Aucun commentaire:
Enregistrer un commentaire