如何用前面的最后一个非缺失值填充NA?

3

我的数据框中有一列(V5)的所有值都是缺失值:

> df
# A tibble: 7 × 5
     V1    V2    V3    V4 V5   
  <dbl> <dbl> <dbl> <dbl> <lgl>
1  1.19  2.45  0.83  0.87 NA   
2  1.13  0.79  0.68  5.43 NA   
3  1.18  1.09  1.04 NA    NA   
4  1.11  1.1   4.24 NA    NA   
5  1.16  1.13 NA    NA    NA   
6  1.18 NA    NA    NA    NA   
7  1.44 NA     9.17 NA    NA

我希望在第五列(V5)中填入前面最近的非缺失值:

> df1
# A tibble: 7 × 5
     V1    V2    V3    V4    V5
  <dbl> <dbl> <dbl> <dbl> <dbl>
1  1.19  2.45  0.83  0.87  0.87
2  1.13  0.79  0.68  5.43  5.43
3  1.18  1.09  1.04 NA     1.04
4  1.11  1.1   4.24 NA     4.24
5  1.16  1.13 NA    NA     1.13
6  1.18 NA    NA    NA     1.18
7  1.44 NA     9.17 NA     9.17

类似的帖子,但没有一个能够解决这个问题。因此,任何线索将不胜感激。

以下是dput:

structure(list(V1 = c(1.19, 1.13, 1.18, 1.11, 1.16, 1.18, 1.44
), V2 = c(2.45, 0.79, 1.09, 1.1, 1.13, NA, NA), V3 = c(0.83, 
0.68, 1.04, 4.24, NA, NA, 9.17), V4 = c(0.87, 5.43, NA, NA, NA, 
NA, NA), V5 = c(NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
-7L), class = c("tbl_df", "tbl", "data.frame"))
3个回答

3

你可以使用

library(dplyr)
df %>% 
  mutate(V5 = coalesce(V4, V3, V2, V1))

这将返回

# A tibble: 7 x 5
     V1    V2    V3    V4    V5
  <dbl> <dbl> <dbl> <dbl> <dbl>
1  1.19  2.45  0.83  0.87  0.87
2  1.13  0.79  0.68  5.43  5.43
3  1.18  1.09  1.04 NA     1.04
4  1.11  1.1   4.24 NA     4.24
5  1.16  1.13 NA    NA     1.13
6  1.18 NA    NA    NA     1.18
7  1.44 NA     9.17 NA     9.17

或者更一般地来自于https://github.com/tidyverse/funs/issues/54#issuecomment-892377998

df %>% 
  mutate(V5 = do.call(coalesce, rev(across(-V5))))

https://github.com/tidyverse/funs/issues/54#issuecomment-1096449488

这段内容涉及IT技术。请参考上述链接了解详情。
df %>% 
  mutate(V5 = coalesce(!!!rev(select(., -V5))))

2
你也可以尝试这样做,但另一种解决方案更加优雅,当然是最值得推荐的:
library(dplyr)

df %>%
  rowwise() %>%
  mutate(V5 = last(c_across(V1:V4)[!is.na(c_across(V1:V4))]))

# A tibble: 7 x 5
# Rowwise: 
     V1    V2    V3    V4    V5
  <dbl> <dbl> <dbl> <dbl> <dbl>
1  1.19  2.45  0.83  0.87  0.87
2  1.13  0.79  0.68  5.43  5.43
3  1.18  1.09  1.04 NA     1.04
4  1.11  1.1   4.24 NA     4.24
5  1.16  1.13 NA    NA     1.13
6  1.18 NA    NA    NA     1.18
7  1.44 NA     9.17 NA     9.17

2

使用 base R

df1$V5 <- as.data.frame(df1[1:4])[cbind(seq_len(nrow(df1)), 
     max.col(!is.na(df1), "last"))]
df1$V5
[1] 0.87 5.43 1.04 4.24 1.13 1.18 9.17

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接