In my dataset I have many categorical variables and I want to pick up associations between these variables. However, I am struggling to figure out how to automate it so I do not have to do a Chi sq test between every pair.
For example, let's say I have a dataframe.
#Create variables
set.seed(123)
fruit <-c('apple','orange','orange','pear')
fav_number <- seq(from=1,to=4,1)
place <- c('nigeria','india','usa','mexico')
weather <- c('summer','winter','spring','summer')
car <- c('bmw','mercedes','honda','honda')
#Create dataframe
df <- as.data.frame(cbind(fruit,fav_number,place,weather,car))
#Convert all columns to factors
df[sapply(df, is.character)] <- lapply(df[sapply(df, is.character)],
as.factor)
So my output/df looks like:
fruit fav_number place weather car
1 apple 1 nigeria summer bmw
2 orange 2 india winter mercedes
3 orange 3 usa spring honda
4 pear 4 mexico summer honda
I can do a Chi sq test between two variables with:
chisq.test(table(df$place,df$fav_number))
But I want to do the same test with every variable with the other. The output I'm looking for is similar to what you would get with a corr matrix with continuous variables.