Search code examples
rdataframeif-statementmultiple-columns

How to add a new column to a dataframe in condition on the previous row of the desired value to be all NAs?


I have this dataframe df and the vector z

df = data.frame(x =c(letters[1:3],NA,NA,'part1',letters[4:5],NA,NA,'part2',
letters[6:7]),
                y = c('p1','p2','p3',NA,NA,'---','p4',
'p5',NA,NA,'---','p6','p7') )


z = 5:6

and I want to create a column the is called score with part1 has the score 5 and part2 has the score 6. the condition is that the row before each part is composed of NAs. The other values in the score column would be NAs. Appreciate the help.

the expected output

       x    y   score
1      a   p1    NA
2      b   p2    NA
3      c   p3    NA
4   <NA> <NA>    NA
5   <NA> <NA>    NA
6  part1  ---    5
7      d   p4    NA
8      e   p5    NA
9   <NA> <NA>    NA
10  <NA> <NA>    NA
11 part2  ---    6
12     f   p6    NA
13     g   p7    NA

Solution

  • This will apply the values in z to the rows of df where x contains 'part':

    df$score[grepl('part', df$x)] <- z