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

前端 未结 4 563
盖世英雄少女心
盖世英雄少女心 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 00:58

    Create some dummy data:

    dataset <- data.frame(
        spend=10*runif(100),
        email=sample(c("yahoo","gmail","hotmail","aol"),100,replace=TRUE),
        browser=sample(c("Mozilla","IE","Chrome","Opera"),100,replace=TRUE),
        country=sample(c("USA","Canada","China","Australia",
          "Egypt","S.Korea","Brazil"),100,replace=TRUE))
    

    Average the spend per combination:

    with(dataset,by(spend,list(email,browser,country),mean))
    

    Note the NAs for combinations without entries.

    Or turn this into a three-dimensional array:

    as.table(with(dataset,by(spend,list(email,browser,country),mean)))
    

提交回复
热议问题