gpt4 book ai didi

R 使用 dcast、melt 和 concatenation reshape 数据框

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

我有一个数据框如下:

mydf <- data.frame(Term = c('dog','cat','lion','tiger','pigeon','vulture'), Category = c('pet','pet','wild','wild','pet','wild'),
Count = c(12,14,19,7,11,10), Rate = c(0.4,0.7,0.3,0.6,0.1,0.8), Brand = c('GS','GS','MN','MN','PG','MN') )

产生数据框:

     Term Category Count Rate Brand
1 dog pet 12 0.4 GS
2 cat pet 14 0.7 GS
3 lion wild 19 0.3 MN
4 tiger wild 7 0.6 MN
5 pigeon pet 11 0.1 PG
6 vulture wild 10 0.8 MN

我希望将此数据框转换为以下 resultDF

Category         pet              wild              
Term dog,cat,pigeon lion,tiger,vulture
Countlessthan13 dog,pigeon tiger,vulture
Ratemorethan0.5 cat tiger,vulture
Brand GS,PG MN

行标题表示像 Countlessthan13 这样的操作意味着将 Count < 13 应用于术语然后分组。另请注意,品牌名称是独一无二的,不会重复。

我已经尝试过 dcast 和 melt...但没有得到想要的结果。

最佳答案

我们可以使用 data.table 来做到这一点.将 'data.frame' 转换为 'data.table' ( setDT(mydf) ),按 'Category' 分组,按 paste 创建一些汇总列正在 unique 'Count' 小于 13 或 'Rate' 大于 0.5 的 'Term' 值,以及 paste正在 unique “品牌”的元素。

library(data.table)
dt <- setDT(mydf)[, .(Term = paste(unique(Term), collapse=","),
Countlesstthan13 = paste(unique(Term[Count < 13]), collapse=","),

Ratemorethan0.5 = paste(unique(Term[Rate > 0.5]), collapse=","),
Brand = paste(unique(Brand), collapse=",")), by = Category]

从汇总数据集 ('dt') 中,我们 melt通过将 'id.var' 指定为 'Category' 为 'long' 格式,然后 dcast它回到“宽”格式。

dcast(melt(dt, id.var = "Category", variable.name = "category"),
category ~Category, value.var = "value")
# category pet wild
#1: Term dog,cat,pigeon lion,tiger,vulture
#2: Countlesstthan13 dog,pigeon tiger,vulture
#3: Ratemorethan0.5 cat tiger,vulture
#4: Brand GS,PG MN

关于R 使用 dcast、melt 和 concatenation reshape 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39031705/

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