Search code examples
rdataframefor-loopgroupmutate

Creating a new column using a condition and loop


let's assume I have a dataset similar to this:

df1 <- data.frame(age==1:10, year==2004)
df2 <- data.frame(age==1:10, year==2005)
df3 <- data.frame(age==1:10, year==2006)
df <- rbind(df1,df2,df3) 
df <- df %>% mutate(group=case_when(age %in% c(1:3) & year==2004 ~ "A"))

I want those who are 2, 3 and 4 years old in 2005 to be in group A, and those who are 3, 4 and 5 years old in 2006 to be in group A. How do I do this using a for-loop?


Solution

  • You can use ifelse here instead of a for loop. If I understand you correctly, you want anyone born between 2001 and 2003 to be group A, in which case you can do

    df %>% mutate(group = ifelse((year - age) %in% 2001:2003, 'A', 'Not A'))
    #>    age year group
    #> 1    1 2004     A
    #> 2    2 2004     A
    #> 3    3 2004     A
    #> 4    4 2004 Not A
    #> 5    5 2004 Not A
    #> 6    6 2004 Not A
    #> 7    7 2004 Not A
    #> 8    8 2004 Not A
    #> 9    9 2004 Not A
    #> 10  10 2004 Not A
    #> 11   1 2005 Not A
    #> 12   2 2005     A
    #> 13   3 2005     A
    #> 14   4 2005     A
    #> 15   5 2005 Not A
    #> 16   6 2005 Not A
    #> 17   7 2005 Not A
    #> 18   8 2005 Not A
    #> 19   9 2005 Not A
    #> 20  10 2005 Not A
    #> 21   1 2006 Not A
    #> 22   2 2006 Not A
    #> 23   3 2006     A
    #> 24   4 2006     A
    #> 25   5 2006     A
    #> 26   6 2006 Not A
    #> 27   7 2006 Not A
    #> 28   8 2006 Not A
    #> 29   9 2006 Not A
    #> 30  10 2006 Not A
    

    Created on 2022-10-08 with reprex v2.0.2