How do you map every combination of categorical variables in R?

前端 未结 4 567
盖世英雄少女心
盖世英雄少女心 2021-01-07 00:32

Using R, is there a way to take a data set and map out every possible combination of every categorical variable?

For example, let\'s say I had 10,000 rows of custom

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-07 01:03

    Here's a method that utilizes dplyr

    require(magrittr)
    require(dplyr)    
    
    set.seed(123)
    dat = data.frame(email=sample(c("yahoo", "gmail"), 10000, replace=T),
                     browser=sample(c("mozilla", "ie"), 10000, replace=T),
                     country=sample(c("usa", "canada"), 10000, replace=T),
                     money=runif(10000))  
    dat %>%
      group_by(email, browser, country) %>%
      summarize(mean = mean(money))
    # email browser country      mean
    # 1 gmail      ie  canada 0.5172424
    # 2 gmail      ie     usa 0.4921908
    # 3 gmail mozilla  canada 0.4934892
    # 4 gmail mozilla     usa 0.4993923
    # 5 yahoo      ie  canada 0.5013214
    # 6 yahoo      ie     usa 0.5098280
    # 7 yahoo mozilla  canada 0.4985357
    # 8 yahoo mozilla     usa 0.4919743
    

    EDIT: if you want to pass a list into group_by(), you'll need to use the not-non-standard evaluation counterpart, regroup(). For example,

    mylist <- list("email", "browser", "country")
    dat %>%
      regroup(mylist) %>%
      summarize(mean = mean(money))
    

    also see dplyr: How to use group_by inside a function?

提交回复
热议问题