我有如下数据,我想知道从两个以上品牌购买的人所占的百分比:
hh_code brand
3032145 536
3032145 53
3032145 534
324063 536
204128 53
84787 536我想了解每个家庭购买的品牌数量,比如:
hh_code unique_ brand
3032145 3
847827 1
204128 1
84787 1我试过使用表格,但它只是给我频率。会感谢你的任何见解!
发布于 2016-07-18 16:48:19
我们可以使用data.table
library(data.table)
setDT(df1)[, .(unique_brand = uniqueN(brand)), by = hh_code]
# hh_code unique_brand
#1: 3032145 3
#2: 324063 1
#3: 204128 1
#4: 84787 1发布于 2016-07-18 17:36:06
使用tapply的简单的基本R解决方案
num_brands <- tapply(df$brand, df$hh_code, length)
ge2_brands <- num_brands > 2https://stackoverflow.com/questions/38442063
复制相似问题