我有一个包含2列ID和Product的数据框,如下所示:
ID Product
A Clothing, Clothing Food, Furniture, Furniture
B Food,Food,Food, Clothing
C Food, Clothing, Clothing我只需要为每个ID提供唯一的产品,例如:
ID Product
A Clothing, Food, Furniture
B Food, Clothing
C Food, Clothing如何使用R完成此操作
发布于 2016-02-09 15:58:04
如果数据集中有多个分隔符,一种方法是使用所有分隔符拆分“Product”列,获取unique,然后将其按“ID”分组在一起(toString)。这里我们使用data.table方法。
library(data.table)
setDT(df1)[, list(Product= toString(unique(strsplit(Product,
',\\s*|\\s+')[[1]]))), by = ID]
# ID Product
#1: A Clothing, Food, Furniture
#2: B Food, Clothing
#3: C Food, Clothinghttps://stackoverflow.com/questions/35286596
复制相似问题