如果接受另外 3 列中的条件,则在同一列的下一行中用 NA 替换值
Replace value with NA in next row on the same column if condition in another 3 columns are accepted
如果第 3 列的条件与上一行相同,我需要将同一列的下一行中的数据替换为 NA。这个条件的代码怎么写?
例如我有这样的示例数据
store camp var value
a 1 ax 200
b 2 bx 100
a 1 ax 200
b 2 bx 100
c 2 cx 100
a 1 ax 200
c 2 cx 100
变成这样
store camp var value
a 1 ax 200
b 2 bx 100
a 1 ax NA
b 2 bx NA
c 2 cx 100
a 1 ax NA
c 2 cx NA
有没有办法做到这一点?提前谢谢
您可以使用重复的 - 这与您拥有的相匹配
x <- data.frame(
store = c( "a","b","a","b","c","a","c"),
camp = c(1,2,1,2,2,1,2),
var = c( "ax","bx","ax","bx","cx","ax","cx"),
value = c( 200,100,200,100,100,200,10))
x[ duplicated( x[ , c( "store","camp","var")]) ,"value"] <- NA
如果第 3 列的条件与上一行相同,我需要将同一列的下一行中的数据替换为 NA。这个条件的代码怎么写?
例如我有这样的示例数据
store camp var value
a 1 ax 200
b 2 bx 100
a 1 ax 200
b 2 bx 100
c 2 cx 100
a 1 ax 200
c 2 cx 100
变成这样
store camp var value
a 1 ax 200
b 2 bx 100
a 1 ax NA
b 2 bx NA
c 2 cx 100
a 1 ax NA
c 2 cx NA
有没有办法做到这一点?提前谢谢
您可以使用重复的 - 这与您拥有的相匹配
x <- data.frame(
store = c( "a","b","a","b","c","a","c"),
camp = c(1,2,1,2,2,1,2),
var = c( "ax","bx","ax","bx","cx","ax","cx"),
value = c( 200,100,200,100,100,200,10))
x[ duplicated( x[ , c( "store","camp","var")]) ,"value"] <- NA