gpt4 book ai didi

使用 dplyr 按列类别将具有多行值的数据框重组为单行

转载 作者:行者123 更新时间:2023-12-04 10:56:24 24 4
gpt4 key购买 nike

以下是 dataframe 子集的示例我在 R有多个公司的信息超过多个 rows按类别 - company_name , no_workers, product , addresscontact_person )

comp_df <- structure(list(desc = c("AAA", "Company", "Ltd", "fish", "344", 
"12", "West", "Road", "Bob C", "BBB", "Enteprises", "vegetables",
"12", "North", "Perak", "Simon T", "EF", "Industries", "cement",
"8800", "Green", "Lane", "Singapore", "Sylvia P"), category = c("company_name",
"company_name", "company_name", "product", "no_workers", "address",
"address", "address", "contact_person", "company_name", "company_name",
"product", "no_workers", "address", "address", "contact_person",
"company_name", "company_name", "product", "no_workers", "address",
"address", "address", "contact_person")), row.names = c(NA, -24L
), class = c("tbl_df", "tbl", "data.frame"))

有没有简单的方法可以添加到我的 dplyr pipe 一个函数来转换上面的 dataframe变成类似下面的东西

enter image description here

最佳答案

假设在您的原始数据框中,在 category company_name 的第一个值的列在每一组中都标志着一个新组的开始,你可以这样做:

library(dplyr)
library(tidyr)

comp_df %>%
group_by(category, grp = cumsum(category == "company_name" & lag(category, default = "") != "company_name")) %>%
summarise(desc = paste(desc, collapse = " ")) %>%
pivot_wider(id_cols = grp, names_from = category, values_from = desc)

# A tibble: 3 x 6
grp address company_name contact_person no_workers product
<int> <chr> <chr> <chr> <chr> <chr>
1 1 12 West Road AAA Company Ltd Bob C 344 fish
2 2 North Perak BBB Enteprises Simon T 12 vegetables
3 3 Green Lane Singapore EF Industries Sylvia P 8800 cement

关于使用 dplyr 按列类别将具有多行值的数据框重组为单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59149609/

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