【发布时间】:2021-01-09 04:39:23
【问题描述】:
这是一个数据框
# 5 companies observed each day for 10 days
df <- tibble(
company = rep(LETTERS[1:5], 10),
value = rep(sample(100, 5), 10),
date = rep(seq(as.Date("2020-01-01"), as.Date("2020-01-10"), 1), each = 5)
)
df
现在数据发生了一些变化,公司的一些 E 行被删除了。
df_error <- df[-c(5, 10, 15, 20), ]
df_error
添加回 E 行的最简单的 Tidyverse 方法是什么。 价值无所谓。 E行的日期与上面的D行相同。
我从以下内容开始,但不知道如何继续:
# Find all D occurrences
e_idx <- which(df_error$company == "D")
e_idx
# If there is not an E in the next row, get the index. These need E rows below each index value.
rows_need_e_below <- ifelse(df_error[e_idx + 1, 1] != "E", e_idx, NA)
rows_need_e_below
【问题讨论】: