答案 0 :(得分:1)
我们可以尝试na.locf
(假设在阅读excel文件后,' B'中的空白为NA
)
library(zoo)
df1$C <- na.locf(na.locf(df1$B, na.rm=FALSE), fromLast=TRUE)
df1$C
#[1] 20 20 20 20 30 30 30
数据:
df1 = structure(list(A = 1:7, B = c(NA, NA, 20, NA, NA, 30, NA)), .Names = c("A",
"B"), row.names = c(NA, -7L), class = "data.frame")