I want make a ‘new column’ with values from ‘number’ only for ‘sp.name’ (grouping variable) where both responses ‘young’ and ‘adult’ are present; if not, enter 0 in the ‘new column’.
df <- data.frame(sp.name= c('a','a', 'b', 'b' ,'c', 'd' ),
number=c(2,2,3, 3,4,4),
stage= c('adult', 'young', 'young','adult', 'adult', 'young'))
Here is what I tried.
df %>%
group_by(sp.name) %>%
mutate(new_column = ifelse('young' %in% stage & 'adult' %in% stage,
number[stage == 'adult'], 0))
But my code reproduces value for ‘young’ also to the new column, I want only values related to ‘adult’.
Desired output:
name | number | stage | new_column |
---|---|---|---|
a | 2 | adult | 2 |
a | 2 | young | 0 |
b | 3 | young | 0 |
b | 3 | adult | 3 |
c | 4 | adult | 0 |
d | 4 | young | 0 |
>Solution :
df %>%
group_by(sp.name) %>%
mutate(new = (any(stage == 'adult') & any(stage == 'young') & stage == 'adult') * number)
#> # A tibble: 6 x 4
#> # Groups: sp.name [4]
#> sp.name number stage new
#> <chr> <dbl> <chr> <dbl>
#> 1 a 2 adult 2
#> 2 a 2 young 0
#> 3 b 3 young 0
#> 4 b 3 adult 3
#> 5 c 4 adult 0
#> 6 d 4 young 0