如何转换数据以查找具有相同值的索引

How to Transform Data to Find Index with Same Value

我打算找到购买过完全相同产品的客户,

我拥有的数据是客户的行为——他们购买了什么。

我提供的示例是我的数据的简化版本。客户通常会购买 10 到 20 件产品。消费者可以选择购买的产品大约有 50 种。

我真的很困惑什么是将我的数据转换为我喜欢的输出的简单方法。 你能给我什么建议吗?谢谢

输入:

structure(list(Customer_ID = 1:6, Products = c("Apple, Beer, Diaper", 
"Beer, Apple", "Beer, Apple, Diaper, Diaper", "Apple, Diaper", 
"Diaper, Apple", "Apple, Diaper, Beer, Beer")), .Names = c("Customer_ID", 
"Products"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), spec = structure(list(cols = structure(list(Customer_ID = structure(list(), class = c("collector_integer", 
"collector")), Products = structure(list(), class = c("collector_character", 
"collector"))), .Names = c("Customer_ID", "Products")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

输出:

structure(list(`Products Bought` = c("Apple, Beer, Diaper", "Apple, Diaper"
), Customer_ID = c("1, 3, 6", "4, 5")), .Names = c("Products Bought", 
"Customer_ID"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-2L), spec = structure(list(cols = structure(list(`Products Bought` = structure(list(), class = c("collector_character", 
"collector")), Customer_ID = structure(list(), class = c("collector_character", 
"collector"))), .Names = c("Products Bought", "Customer_ID")), 
    default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

我怀疑您是否希望以更有用的方式构建数据。在任何情况下,tidyverse 都是一种有助于思考任务的方法。

如前所述,发布代码供其他人开始使用可以节省他们的时间并让您更快得到答案。

library(dplyr)
library(stringr)
library(tidyr)

d <- data_frame(id=c(1,2,3,4,5,6)
     , bought=c('Apple, Beer, Diaper','Apple, Beer', 'Apple, Beer, Diaper, Diaper'
               , 'Apple, Diaper', 'Diaper, Apple', 'Apple, Diaper, Beer, Beer'))

d %>% 
## Unnest the values & take care of white space
## - This is the better data structure to have, anyways
mutate(buy=str_split(bought,',')) %>% 
unnest(buy) %>% mutate(buy=str_trim(buy)) %>% select(-bought) %>%

## Get distinct (and sort?)
distinct(id, buy) %>% arrange(id, buy) %>%

## Aggregate by id
group_by(id) %>% summarize(bought=paste(buy,collapse=', ')) %>% ungroup %>%

## Count
group_by(bought) %>% summarize(ids=paste(id,collapse=',')) %>% ungroup

编辑:参考 this SO post 以在 dplyr

中更快/更清晰地获得不同的组合

使用给定的 input 数据和 data.table,这可以写成(相当复杂)"one-liner":

dcast(unique(setDT(input)[, strsplit(Products, ", "), Customer_ID])[
  order(Customer_ID, V1)], 
  Customer_ID ~ ., paste, collapse = ", ")[
    , .(Customers = paste(Customer_ID, collapse = ", ")), .(Products = .)]
#              Products Customers
#1: Apple, Beer, Diaper   1, 3, 6
#2:         Apple, Beer         2
#3:       Apple, Diaper      4, 5

请注意,OP 删除了第二行,只有一位客户来自 预期的输出,但没有提到任何过滤问题输出的标准。

输入数据

(由 OP 给出):

input <- structure(list(Customer_ID = 1:6, Products = c("Apple, Beer, Diaper", 
"Beer, Apple", "Beer, Apple, Diaper, Diaper", "Apple, Diaper", 
"Diaper, Apple", "Apple, Diaper, Beer, Beer")), .Names = c("Customer_ID", 
"Products"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), spec = structure(list(cols = structure(list(Customer_ID = structure(list(), class = c("collector_integer", 
"collector")), Products = structure(list(), class = c("collector_character", 
"collector"))), .Names = c("Customer_ID", "Products")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))