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

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

    That there is:

    expand.grid(gender = c("male", "female"), tShirtSize = c("xs", "s","m","l","xl"))

    Will return all the combinations in a dataframe. For the summary stats, try aggregate, e.g:

    country = sample(c("america", "canadian"), 30, replace = TRUE)
    gender = sample(c("male", "female"), 30, replace = TRUE)
    x = abs(rnorm(30) * 1000)
    aggregate(data.frame(x), by = list(country, gender), FUN = mean)
    

    I run into errors if there are columns with strings in the dataframe, so I'd subset out the columns with numeric values.

提交回复
热议问题