Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make new colum based on values in two different columns by group

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’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading