how can you use groupby and casewhen to impute a new value in a table?

This is probably not the most efficient way of solving this, but here it goes (I would also welcome any other way of achieving the same thing):

df$dv = 0

for (i in seq(1, nrow(df),by = 3)){
  if(df$score[i] == 100)
  {df$dv[i] = 1}
  if(df$score[i+1] == 100)
  {df$dv[i] = 2}
  if(df$score[i+2] == 100)
  {df$dv[i] = 4}

dv is a new column that stores updated scores. I then just removed score column with a subset function.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top