我有以下数据集
#datset
id attributes value
1 a,b,c 1
2 c,d 0
3 b,e 1
我希望从中创建一个数据透视表,并为属性分配二进制值(如果存在,则为属性分配1,否则为它们分配0)。我理想的输出如下:
#output
id a b c d e Value
1 1 1 1 0 0 1
2 0 0 1 1 0 0
3 0 1 0 0 1 1
任何提示都非常感谢。
我们通过分拆“属性”列“”得到的频率mtabulate
从qdapTools
和cbind
与第一和第三列。
library(qdapTools)
cbind(df1[1], mtabulate(strsplit(df1$attributes, ",")), df1[3])
# id a b c d e value
#1 1 1 1 1 0 0 1
#2 2 0 0 1 1 0 0
#3 3 0 1 0 0 1 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句