我在R环境中有一个时间序列数据集。 变量CB_Day在某些日期等于MPD,在大多数日期等于0。
我要删除除MPD天和前10天之外的所有行。
我尝试了 subset , head()和 tail(),但是它们不起作用。
有人可以告诉我删除记录的正确命令是什么 根据我在R中的状况?
结果应该是整个表以及所有其他列。只有行需要删除。
答案 0 :(得分:2)
如果我做对了,那么类似的东西应该会帮助...
# create data where CB_Day is always 0 (please provide reproducible data next time)
df <- data.frame(MPD = 1:100, CB_Day = rep(0, 100))
# sometimes CB_Day is same as MPD
df$CB_Day[c(20, 70)] <- df$MPD[c(20, 70)]
# Find where both are same
same <- which(df$MPD== df$CB_Day)
# create vectors with "10 rows before CB_Day and MPD are same" to the row where they are same
keep <- sapply(same, function(x){(x-10):x})
# make it a vector instead of a matrix
keep <- unlist(keep)
# select the rows
df[keep, ]