我正在合并两个数据帧,如下所示:
data_merged <- full_join(df1, df2, by=c("col1","col2")) %>%
fill(everything(), .direction = 'down')但是,在新合并的数据框中有一列我不想填充(比如col3)。此行需要保留其NA值。我尝试过用select来做这件事,但失败了,我也想过把它的一部分变成tibble,但无法利用这个想法。
有谁有什么想法吗?
发布于 2020-09-12 04:20:08
试试这个:
data.frame(col1 = 1:10, col2 = c(1, NA), col3 = c(2,NA))%>%
fill(!col3, .direction = 'down')
# col1 col2 col3
# 1 1 1 2
# 2 2 1 NA
# 3 3 1 2
# 4 4 1 NA
# 5 5 1 2
# 6 6 1 NA
# 7 7 1 2
# 8 8 1 NA
# 9 9 1 2
# 10 10 1 NA发布于 2020-09-12 04:36:43
我们也可以使用zoo中的na.locf
library(zoo)
df1$col3 <- na.locf0(df1$col3)数据
df1 <- data.frame(col1 = 1:10, col2 = c(1, NA), col3 = c(2,NA))https://stackoverflow.com/questions/63853610
复制相似问题