I have two dataframes, one where id is a primary identifier and the other is long data. I merged both and so I have long data (this can be reversed). I grouped the data based on id
and want to loop over each group, generating a new variable value for each row in a group based on some condition. For instance, I want to loop over the group with id = '0000'
and for each row in that group, if alx
is greater than 80, my new_variable
= True
Please see sample data below.
structure( list(id = c("0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000"), sex = c("F", "F", "F", "F", "F", "F", "F", "F", "F", "F"), age_fcat = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), levels = c("18-34", "35-44", "45-54", "54-64", "65+" ), class = "factor"), avg_s = c(161, 161, 155.666666666667, 146, 140, 144, 139, 173, 152.875, 156.5), alx = c(88, 88, 90.3333333333333, 76, 78, 73, 86, 97, 82, 97.2), class = c("tbl_df", "tbl", "data.frame") )