请注意,我的问题与this one不同
在下面的数据中,我想填充数值列和字符列,它们都是NA,基于在NA之前的邮政编码的areacode和type是在NA之后的相同的areacode和type的条件。
一句话:“因为zipcode 1002有黏土,zipcode 1004有黏土,我们假设zipcode 1003有黏土。”
我想使用this approach,但是na.fill只填充数值。
dat <- structure(list(zipcode = c(1001, 1002, 1003, 1004), areacode = c(4,
4, NA, 4), type = structure(c(3L, 3L, NA, 3L), .Label = c("",
"sand", "clay", "na2"), class = "factor"), region = c(3, 3,
NA, 3)), class = c("data.table", "data.frame"), row.names = c(NA,
-4L))
zipcode areacode type region
1: 1001 4 clay 3
2: 1002 4 clay 3
3: 1003 NA <NA> NA
4: 1004 4 clay 3
dat2 <- structure(list(zipcode = c(1001, 1002, 1003, 1004), areacode = c(4,
4, NA, 1), type = structure(c(3L, 3L, NA, 2L), .Label = c("",
"sand", "clay", "na2"), class = "factor"), region = c(3, 3, NA,
3)), class = c("data.table", "data.frame"), row.names = c(NA,
-4L))
zipcode areacode type region
1: 1001 4 clay 3
2: 1002 4 clay 3
3: 1003 NA <NA> NA
4: 1004 1 sand 3最好的方法是什么?
期望输出dat
zipcode areacode type region
1: 1001 4 clay 3
2: 1002 4 clay 3
3: 1003 4 clay 3
4: 1004 4 clay 3期望输出dat2
zipcode areacode type region
1: 1001 4 clay 3
2: 1002 4 clay 3
3: 1003 NA <NA> NA
4: 1004 1 sand 3编辑:
下面的内容是不够的,因为它将填写clay,即使第四行表示sand。
dat2 %>%
fill(areacode, type, region)
zipcode areacode type region
1: 1001 4 clay 3
2: 1002 4 clay 3
3: 1003 4 clay 3
4: 1004 1 sand 3
dat2[, lapply(.SD, zoo::na.locf)]
zipcode areacode type region
1: 1001 4 clay 3
2: 1002 4 clay 3
3: 1003 4 clay 3
4: 1004 1 sand 3发布于 2022-04-21 07:09:23
使用dplyr
library(dplyr)
dat2 |>
mutate(type = as.character(type)) |>
mutate(across(2:4,
~ ifelse(is.na(.) & lag(areacode) == lead(areacode) & lag(type) == lead(type),
lag(.),
.)))
zipcode areacode type region
1 1001 4 clay 3
2 1002 4 clay 3
3 1003 NA <NA> NA
4 1004 1 sand 3
dat |>
mutate(type = as.character(type)) |>
mutate(across(2:4,
~ ifelse(is.na(.) & lag(areacode) == lead(areacode) & lag(type) == lead(type),
lag(.),
.)))
zipcode areacode type region
1 1001 4 clay 3
2 1002 4 clay 3
3 1003 4 clay 3
4 1004 4 clay 3https://stackoverflow.com/questions/71950096
复制相似问题