gpt4 book ai didi

r - 计算 R 中唯一值的交叉表

转载 作者:行者123 更新时间:2023-12-01 15:33:17 25 4
gpt4 key购买 nike

我的数据由 3 列组成: - 部分 - 类别 - 产品编号

如何创建一个交叉表(“分割”作为列,“类别”作为行)来计算“产品编号”的唯一值强>”(下面的例子)?

      SEG1  SEG2  SEG3
CAT1 X
CAT2
CAT3

X - CAT1 和 SEG1 中唯一值的数量

示例数据

CAT<-c("CAT1","CAT3","CAT3","CAT1","CAT2","CAT3","CAT3","CAT3","CAT3","CAT2") 
SEG<-c("SEG1","SEG3","SEG3","SEG2","SEG2","SEG2","SEG3","SEG3","SEG2","SEG2")
PRODUCT<-c("a","a","a","a","d","e","b","c","a","a")
data<-cbind(CAT,SEG,PRODUCT)

提前非常感谢!此致,巴特克

最佳答案

您可以简单地计算数据的交叉表,但无需重复行,以确保只计算唯一的产品编号:

nodup <- which(!duplicated(data))
table(data[nodup, "CAT"],data[nodup, "SEG"])

SEG1 SEG2 SEG3
CAT1 1 1 0
CAT2 0 2 0
CAT3 0 2 3

关于r - 计算 R 中唯一值的交叉表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29823959/

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