我有以下数据帧df1。我如何保存最后一次出现的w.
之后的列值,例如对于id 6
来说最后一次出现的w
是在t8时,因此,我想保存出现的值在t9
处以及在新数据帧中最后一个w
的位置。
输入:
id t1 t2 t3 t4 t5 t6 t7 t6 t8 t9
1 s s s w r r t t w s
2 s s s o o w t t o s
3 s s s o w w t t o s
4 s s s o o w t t o s
5 w s s s s s w w s s
6 s s s w t t w w w s
输出:
id t1 t2 t3 t4 t5 t6 t7 t6 t8 t9
1 w s
2 w t t o s
3 w t t o s
4 w t t o s
5 w s s
6 w s
样本数据:
df1<-structure(list(id=c(1,2,3,4,5,6), t1=c("s","s","s","s","w","s"), t2=c("s","s","s","s","s","s"),t3 = c("s","s","s","s","s","s"),
t4 = c("w","o","o","o","s","w"), t5 = c("r","o","w","o","s","t"), t6 = c("r","w","w","w","s","t"),
t7 = c("t","t","t","t","w","w"),t6 = c("t","t","t","t","w","w"), t8 = c("w","o","o","o","s","w"), t9=c("s","s","s","s","s","s")), row.names = c(NA, 6L), class = "data.frame")
答案 0 :(得分:4)
您可以逐行使用apply
:
df1[-1] <- t(apply(df1[-1], 1, function(x) {
ind <- max(which(x == 'w'))
x[seq_len(ind - 1)] <- ''
x
}))
df1
# id t1 t2 t3 t4 t5 t6 t7 t6 t8 t9
#1 1 w s
#2 2 w t t o s
#3 3 w t t o s
#4 4 w t t o s
#5 5 w s s
#6 6 w s
max(which(x == 'w'))
返回该行中最后一次出现的'w'
的索引。
另一种选择是创建需要替换的行和列值矩阵。我们可以将max.col
与ties.method = 'last'
一起使用,这将为您提供最后一次出现的'w'
的索引每一行。现在,我们需要为cols
中的每个值创建一个从2到该数字的序列,以创建可用于替换空白值的矩阵。
cols <- max.col(df1[-1] == 'w', ties.method = 'last')
mat <- do.call(rbind, Map(function(x, y) cbind(x, seq(y)[-1]), 1:nrow(df1), cols))
df1[mat] <- ''