我有以下关于买入、卖出或按兵不动的数据框架:
df <- structure(list(date = structure(c(17448, 17449, 17450, 17451,
17452, 17455, 17456, 17457, 17458, 17459), class = "Date"), Activity = c("Buy",
"Nothing", "Nothing", "Sell", "Nothing", "Sell", "Buy", "Nothing",
"Nothing", "Nothing")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
我想保持卖出/买入,除非之前的指令与之相反.也就是说,如果有两个连续的"卖出"(可能中间没有"任何"),我只想保留第一个(并用"没有"替换第二个).
我如何才能在没有循环的情况下完成这项工作?考虑一个巨大的数据帧
预期输出(仅第6行已更改):
structure(list(date = structure(c(17448, 17449, 17450, 17451,
17452, 17455, 17456, 17457, 17458, 17459), class = "Date"), Activity =
c("Buy",
"Nothing", "Nothing", "Sell", "Nothing", "Nothing", "Buy", "Nothing",
"Nothing", "Nothing")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))