r - 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!!!
解决方案
在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]))
推荐阅读
- spring-boot - 将新记录添加到数据库时更新 vaadin 网格
- android - 从同一个帐户多次在 Android 应用上订购订阅
- java - com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587 超时 -1
- google-apps-script - 将错误消息设置为英语
- design-patterns - 如何在字符串中找到转义字符\?
- java - 逃逸分析是否有助于 JVM 在堆栈上分配数组?
- tornado - 为什么 Tornado 会花时间在 socket.send(data) 中?
- php - chmod():Laravel 中不允许的操作
- emacs - Emacsclient:搜索程序:没有这样的文件或目录,ruby
- parameters - 如何获取视图组件中的参数值?