gpt4 book ai didi

r - 按 ID 列折叠所有列

转载 作者:行者123 更新时间:2023-12-03 21:05:39 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Collapse / concatenate / aggregate a column to a single comma separated string within each group

(4 个回答)


3年前关闭。




我正在尝试做类似于 what's answered here 的事情,这让我成功了 80%。我有一个包含一个 ID 列和多个信息列的数据框。我要卷起来 全部 的其他列,以便每个 ID 只有一行,并且多个条目由例如分号分隔。这是我拥有什么和我想要什么的一个例子。

有:

     ID  info1          info2
1 id101 one first
2 id102 twoA second alias A
3 id102 twoB second alias B
4 id103 threeA third alias A
5 id103 threeB third alias B
6 id104 four fourth
7 id105 five fifth

想:
     ID          info1                          info2
1 id101 one first
2 id102 twoA; twoB second alias A; second alias B
3 id103 threeA; threeB third alias A; third alias B
4 id104 four fourth
5 id105 five fifth

这是用于生成这些的代码:
have <- data.frame(ID=paste0("id", c(101, 102, 102, 103, 103, 104, 105)),
info1=c("one", "twoA", "twoB", "threeA", "threeB", "four", "five"),
info2=c("first", "second alias A", "second alias B", "third alias A", "third alias B", "fourth", "fifth"),
stringsAsFactors=FALSE)
want <- data_frame(ID=paste0("id", c(101:105)),
info1=c("one", "twoA; twoB", "threeA; threeB", "four", "five"),
info2=c("first", "second alias A; second alias B", "third alias A; third alias B", "fourth", "fifth"),
stringsAsFactors=FALSE)

This question问了基本相同的问题,但只有一个“信息”列。我有多个其他列,并希望为所有列执行此操作。

使用 dplyr 执行此操作的奖励积分。

最佳答案

这是使用 summarise_each 的选项(这样可以轻松地将更改应用于除分组变量之外的所有列)和 toString :

require(dplyr)

have %>%
group_by(ID) %>%
summarise_each(funs(toString))

#Source: local data frame [5 x 3]
#
# ID info1 info2
#1 id101 one first
#2 id102 twoA, twoB second alias A, second alias B
#3 id103 threeA, threeB third alias A, third alias B
#4 id104 four fourth
#5 id105 five fifth

或者,如果你想用分号分隔,你可以使用:
have %>%
group_by(ID) %>%
summarise_each(funs(paste(., collapse = "; ")))

关于r - 按 ID 列折叠所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26981385/

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