找到一组无序的唯一字符串

时间:2017-04-03 17:29:29

标签: r data.table

我们说我有一些类似的数据:

> dput(test1)
structure(list(User = c(95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 
95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 113L, 113L, 113L, 
113L, 113L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 
116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 123L, 
123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 
123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 
123L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 
135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 
135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 
135L, 135L, 135L), Daypart6 = c("Afternoon", "Dinner", "Breakfast", 
"Lunch", "Lunch", "Lunch", "Dinner", "Dinner", "Breakfast", "Dinner", 
"Lunch", "Dinner", "Dinner", "Breakfast", "Breakfast", "Breakfast", 
"Breakfast", "Breakfast", "Lunch", "Lunch", "Breakfast", "Lunch", 
"Dinner", "Dinner", "Dinner", "Dinner", "Dinner", "Dinner", "Dinner", 
"Afternoon", "Afternoon", "Afternoon", "Afternoon", "Afternoon", 
"Afternoon", "Afternoon", "Afternoon", "Afternoon", "Afternoon", 
"Afternoon", "Afternoon", "Dinner", "Dinner", "Dinner", "Dinner", 
"Dinner", "Dinner", "Afternoon", "Afternoon", "Afternoon", "Afternoon", 
"Lunch", "Lunch", "Dinner", "Dinner", "Lunch", "Dinner", "Dinner", 
"Dinner", "Dinner", "Lunch", "Lunch", "Lunch", "Lunch", "Lunch", 
"Dinner", "Dinner", "Dinner", "Dinner", "Dinner", "Dinner", "Dinner", 
"Dinner", "Dinner", "Dinner", "Dinner", "Dinner", "Dinner", "Dinner", 
"Dinner", "Dinner", "Lunch", "Dinner", "Dinner", "Evening", "Dinner", 
"Evening", "Dinner", "Evening", "Dinner", "Evening", "Evening", 
"Dinner", "Lunch", "Dinner", "Dinner", "Evening", "Evening", 
"Evening", "Dinner")), .Names = c("Code", "TimeOfDay"), sorted = "User", class = c("data.table", 
"data.frame"), row.names = c(NA, -100L), .internal.selfref = <pointer: 0x2d98fd8>)
>

library(data.table)
test[,uniqueTOD:=paste(unique(TimeOfDay),collapse='/'),'Code']

我得到的东西真的像:

 Breakfast/Dinner
Dinner/Breakfast

在python中,有一种方法可以使用集合制作无序的唯一集合。 R中有类似的东西吗?

注意:这是数据的子集,因此它可能没有像所描述的那样的唯一组合。数据仅用于结构目的。

0 个答案:

没有答案