meitei
January 11, 2023, 7:54pm
1
I have a dataframe ,df . I would like to divide value by 5 when RCM_GCM is either CAN or HAD .
Else, I would like to multiple by 3.
I tried the following.
Thank you
RCM_GCM="HAD"
df=data.frame(
dt=seq(1:5),
value=c(10,15,5,0,55)
)
hist1=df %>%
mutate(valu_his=ifelse(RCM_GCM%in% c("CAN","HAD"),
value/5,
value*3)
)
Here is one approach.
hist1= df %>%
rowwise() %>%
mutate(valu_his = ifelse(RCM_GCM %in% c("CAN","HAD"),
value/5,
value*3)
) %>%
ungroup()
hist1
#> # A tibble: 5 × 3
#> dt value valu_his
#> <int> <dbl> <dbl>
#> 1 1 10 2
#> 2 2 15 3
#> 3 3 5 1
#> 4 4 0 0
#> 5 5 55 11
Created on 2023-01-11 with reprex v2.0.2.9000
1 Like
system
Closed
January 18, 2023, 8:11pm
3
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.