如何使用 R 将列中的 na 替换为第一个非缺失值,而不删除仅包含缺失值的情况?
我有一个包含许多 NA 的长数据框,但我想压缩它,以便所有 NA 在按变量分组时都填充第一个非缺失值 - 但如果观察仅有 NA ,它保留它。在我更新 R 之前,我有一个有效的代码(如下所示),但现在如果其中一列全部为 NA,它会删除行。
这是一个示例数据集:
library(dplyr)
test <- tibble(name = c("J", "C", "J", "C"),
test_1 = c(1:2, NA, NA),
test_2 = c(NA, NA, 3:4),
make_up_test = c(NA, 1, NA, NA))
这是曾经有效的方法 - 但现在删除了一列中只有 NA 的观察结果(参见 J 被删除,因为他只有 NA 用于测试观察)
test %>%
group_by(name) %>%
summarise_all(~first(na.omit(.)))
这就是我希望得到的:
solution <- tibble(name = c("J", "C"),
test_1 = c(1:2),
test_2 = c(3:4),
make_up_test = c(NA, 1))
I have a long data frame that has many NAs, but I want to condenses it so all NAs are filled with the first non-missing value when grouped by a variable--but if the observation only has NAs, it keeps it. Until I updated R, I had a code that worked (shown below), but now it deletes rows if one of their columns is all NAs.
Here's a sample dataset:
library(dplyr)
test <- tibble(name = c("J", "C", "J", "C"),
test_1 = c(1:2, NA, NA),
test_2 = c(NA, NA, 3:4),
make_up_test = c(NA, 1, NA, NA))
And here's what used to work--but now deletes observations that only have NAs in one column (see J getting dropped because he only has NAs for test observation)
test %>%
group_by(name) %>%
summarise_all(~first(na.omit(.)))
This is what I'm hoping to get:
solution <- tibble(name = c("J", "C"),
test_1 = c(1:2),
test_2 = c(3:4),
make_up_test = c(NA, 1))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们使用
na.omit
删除NA
并获取first
元素 - 使用[1]
强制为 < code>NA 如果不存在非 NA 元素-输出
We remove the
NA
withna.omit
and get thefirst
element - use[1]
to coerce toNA
if there are no non-NA elements present-output
这是一种旋转方法:
Here is an approach with pivoting: