Kam*_*gga 6 r dataframe na imputation
我有一个像 DF
现在我想用 15 替换 Col B = NA,因为这是缺失值。C 列第一个 NA 为 14,第二个 NA 为 15。D 列第一个 NA 为 13,第二个 NA 为 14,第三个 NA 为 15。因此数字遵循从上到下或从下到上的顺序。
可重现的样本数据
structure(list(`Col A` = c(11, 12, 13, 14, 15), `Col B` = c(NA,
11, 12, 13, 14), `Col C` = c(NA, NA, 11, 12, 13), `Col D` = c(NA,
NA, NA, 11, 12)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
我认为您可以在以下解决方案中使用tidyverse:
library(dplyr)
library(purrr)
df[1] %>%
bind_cols(map_dfc(2:length(df), function(x) {
df[[x]][which(is.na(df[[x]]))] <- setdiff(df[[1]], df[[x]][!is.na(df[[x]])])
df[x]
}))
# A tibble: 5 x 4
`Col A` `Col B` `Col C` `Col D`
<dbl> <dbl> <dbl> <dbl>
1 11 15 14 13
2 12 11 15 14
3 13 12 11 15
4 14 13 12 11
5 15 14 13 12
Run Code Online (Sandbox Code Playgroud)
或者在基础 R 中我们可以这样做:
do.call(cbind, Reduce(function(x, y) {
i <- which(is.na(df[[y]]))
df[[y]][i] <- sort(setdiff(x, df[[y]]))
df[[y]]
}, init = df[[1]], 2:length(df), accumulate = TRUE)) |>
as.data.frame() |>
setNames(paste0("Col", LETTERS[1:length(df)]))
ColA ColB ColC ColD
1 11 15 14 13
2 12 11 15 14
3 13 12 11 15
4 14 13 12 11
5 15 14 13 12
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |