Home > Net >  How to add a new column to a dataframe in condition on the previous row of the desired value to be a
How to add a new column to a dataframe in condition on the previous row of the desired value to be a

Time:11-10

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

CodePudding user response:

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

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

CodePudding user response:

I would redefine z as a named vector then you can do the following safely:

z = c("part1" = 5, "part2" = 6)
df$score = z[df$x]

#        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
  • Related