gpt4 book ai didi

r - 如何在删除NA值的同时将许多记录合为一体

转载 作者:行者123 更新时间:2023-12-04 13:38:39 25 4
gpt4 key购买 nike

说我有以下数据框df

name <- c("Bill", "Rob", "Joe", "Joe")
address <- c("123 Main St", "234 Broad St", NA, "456 North Ave")
favteam <- c("Dodgers", "Mets", "Pirates", NA)

df <- data.frame(name = name,
address = address,
favteam = favteam)
df

看起来像:
  name       address favteam
1 Bill 123 Main St Dodgers
2 Rob 234 Broad St Mets
3 Joe <NA> Pirates
4 Joe 456 North Ave <NA>

我想要做的是按名称折叠(大写)行(或一般来说,任意数量的分组变量),并且除了NA之外,还具有其他值替换最终数据中的NA值,例如:
df_collapse <- foo(df)

name address favteam
1 Bill 123 Main St Dodgers
2 Rob 234 Broad St Mets
3 Joe 456 North Ave Pirates

最佳答案

这是dplyr的一个选项:

library(dplyr)

df %>%
group_by(name) %>%
summarise_each(funs(first(.[!is.na(.)]))) # or summarise_each(funs(first(na.omit(.))))

#Source: local data frame [3 x 3]
#
# name address favteam
#1 Bill 123 Main St Dodgers
#2 Joe 456 North Ave Pirates
#3 Rob 234 Broad St Mets

并与data.table:
library(data.table)
setDT(df)[, lapply(.SD, function(x) x[!is.na(x)][1L]), by = name]
# name address favteam
#1: Bill 123 Main St Dodgers
#2: Rob 234 Broad St Mets
#3: Joe 456 North Ave Pirates

或者
setDT(df)[, lapply(.SD, function(x) head(na.omit(x), 1L)), by = name]

编辑:

您说在您的实际数据中,每个名称的非NA响应数量不同。在这种情况下,以下方法可能会有所帮助。

考虑以下修改后的样本数据(请看最后一行):
name <- c("Bill", "Rob", "Joe", "Joe", "Joe")
address <- c("123 Main St", "234 Broad St", NA, "456 North Ave", "123 Boulevard")
favteam <- c("Dodgers", "Mets", "Pirates", NA, NA)

df <- data.frame(name = name,
address = address,
favteam = favteam)

df
# name address favteam
#1 Bill 123 Main St Dodgers
#2 Rob 234 Broad St Mets
#3 Joe <NA> Pirates
#4 Joe 456 North Ave <NA>
#5 Joe 123 Boulevard <NA>

然后,您可以使用此data.table方法来获取非NA响应,该响应可能因名称而在数量上有所不同:
setDT(df)[, lapply(.SD, function(x) unique(na.omit(x))), by = name]
# name address favteam
#1: Bill 123 Main St Dodgers
#2: Rob 234 Broad St Mets
#3: Joe 456 North Ave Pirates
#4: Joe 123 Boulevard Pirates

关于r - 如何在删除NA值的同时将许多记录合为一体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28509462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com