首页 > 解决方案 > Replacing all values to 1 after a condition

问题描述

My current data is like below,

df<-data.frame(id=c(1:5),t1=c(NA,1,0,0,0),t2=c(0,1,0,1,0),
t3=c(NA,0,0,0,1),t4=c(NA,NA,NA,0,0))

And the way I'm trying to restructure this is, for each id, if there's a "1" in that row, all the 0s in the subsequent columns would change to 1. (but leaving the NA as an NA).

So for id#1, nothing would change since there's no 1 in that row, but for id#2, after 1 in the column t2, any 0s afterwards would be replaced by 1.

i.e., this is what I'm trying to get at the end:

final<-data.frame(id=c(1:5),t1=c(0,1,0,0,0),t2=c(0,1,0,1,0),
t3=c(NA,1,0,1,1),t4=c(NA,NA,NA,1,1))

I've been trying different ways but nothing seems to work... I'd really appreciate any help!!!

标签: rdataframedata-structures

解决方案


base R我们可以cummax在将 更改为较低值后应用 by rowNA然后将值替换回NA

df[-1] <-  t(apply(replace(df[-1], is.na(df[-1]), -999), 1, cummax)) * 
                NA^(is.na(df[-1]))
df
#  id t1 t2 t3 t4
#1  1 NA  0 NA NA
#2  2  1  1  1 NA
#3  3  0  0  0 NA
#4  4  0  1  1  1
#5  5  0  0  1  1

或使用rowCummaxs来自matrixStats

library(matrixStats)
df[-1] <- rowCummaxs(as.matrix(replace(df[-1], is.na(df[-1]), -999))) * 
             NA^(is.na(df[-1]))

推荐阅读