Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

How to find total frequency by different permutations in R?

A while back I asked a question about how to return the total unique combinations and frequency of customers by brand. This week, I am trying to find the number of customers based on permutations of brands. The permutations cannot repeat themselves.

Here is my sample data:

data_test <- data.frame(CustomerID = c('AA', 
                      'AA', 
                      'AA', 
                      'AA',
                      'BB', 
                      'BB',
                      'CC'),
       Brand = c('A', 
                 'B', 
                 'C', 
                 'D', 
                 'C', 
                 'D',
                 'C'))

And here is what I am trying to get:

# A tibble: 4 x 2
   Combinations             n
   <chr>                    <int>
1  A                        1
2  A-B                      1
3  A-B-C                    1
4  A-B-C-D                  1
5  B                        1
6  B-C                      1
7  B-C-D                    1
8  C                        3
9  C-D                      2
10 D                        2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this?

combn_vec <- function(vec) {
  unlist(lapply(seq_along(vec),
                function(len) apply(combn(sort(unique(vec)), len), 2,
                                    paste, collapse = "-")))
}

library(dplyr)
data_test %>%
  group_by(CustomerID) %>%
  summarize(Combinations = combn_vec(Brand)) %>%
  ungroup() %>%
  count(Combinations)
# # A tibble: 15 x 2
#    Combinations     n
#    <chr>        <int>
#  1 A                1
#  2 A-B              1
#  3 A-B-C            1
#  4 A-B-C-D          1
#  5 A-B-D            1
#  6 A-C              1
#  7 A-C-D            1
#  8 A-D              1
#  9 B                1
# 10 B-C              1
# 11 B-C-D            1
# 12 B-D              1
# 13 C                3
# 14 C-D              2
# 15 D                2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...