Hi community,
I have an example dataset "student_score" where I have variables - age and score for 9 years for 11 different individuals .
I want to change the value in the column score1:score9 based on the value in the age column (age1:age9). The condition would be that if someone is less then age 20, then all the values of their score for that year will not be counted and will thus be converted into "0".
i.e if age1:age9 < 20, then values for those years will be = 0 and won't be counted.
e.g for subjectid "b", their score for the first 3 year will not be counted as during those year their age was 18,19 and 20 years.
I need to do this across a large number of columns so looking for a way to do it at once rather than individually - column by column which i figured out.
Thankyou for the help !
student_score <- data.frame(
subjectid = c("a","b","c","d","e","f","g","h","i","j","k"),
age1 = c(27,18,28,14,11,32,20,19,12,23,34),
age2 = c(28,19,29,15,12,33,21,20,13,24,35),
age3 = c(29,20,30,16,13,34,22,21,14,25,36),
age4 = c(30,21,31,17,14,35,23,22,15,26,37),
age5 = c(31,22,32,18,15,36,24,23,16,27,38),
age6 = c(32,23,33,19,16,37,25,24,17,28,39),
age7 = c(33,24,34,20,17,38,26,25,18,29,40),
age8 = c(34,25,35,21,18,39,27,26,19,30,41),
age9 = c(35,26,36,22,19,40,28,27,20,31,42),
score1 = c(1,4,2,1,1,0,0,1,0,0,0),
score2 = c(1,0,0,1,0,0,2,1,0,2,0),
score3 = c(1,5,2,1,0,0,1,1,1,0,0),
score4 = c(1,1,0,1,2,0,1,1,5,1,1),
score5 = c(0,4,2,1,0,0,0,1,1,1,1),
score6 = c(1,2,0,1,0,2,1,1,0,1,0),
score7 = c(1,1,0,0,1,2,1,0,0,1,1),
score8 = c(1,1,0,0,2,5,1,0,0,3,1),
score9 = c(1,1,3,0,1,0,1,4,5,1,1))