I have this data frame glimpse(df)
Observations: 2,211
Variables: 3
$ city <chr> "Las Vegas", "Pittsburgh", "Las Vegas", "Phoenix", "Las Vegas", "Las Veg...
$ categories <chr> "c(\"Korean\", \"Sushi Bars\")", "c(\"Japanese\", \"Sushi Bars\")", "Tha...
$ is_open <chr> "0", "0", "1", "0", "1", "1", "0", "1", "0", "1", "1", "1", "0", "1", "1...
Here is a small dput()
structure(list(city = c("Las Vegas", "Pittsburgh", "Las Vegas",
"Phoenix", "Las Vegas"), categories = c("c(\"Korean\", \"Sushi Bars\")",
"c(\"Japanese\", \"Sushi Bars\")", "Thai", "c(\"Sushi Bars\", \"Japanese\")",
"Korean"), is_open = c("0", "0", "1", "0", "1")), .Names = c("city",
"categories", "is_open"), row.names = c(NA, 5L), class = "data.frame")
The data consists different cities city
with different cuisines categories
.
I want to make a contingency table to visualize which cuisines are associated with closings (is_opem = 0)
or openings (is_open = 1)
.
I want to do this with a contingency table. To do so I tried this one but I got this error:
xtabs(is_open ~., data = df)
Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument
When I convert the variables as.factor()
I get lots of tables, not one. Is there a way to this so ít´s looking like below?
Categorie/City Las Vegas Pittsburgh
Korean 50/50 30/70
Sushi Bars 40/60 40/60
The numbers in the columns are the frequencies of the closings (is_opem = 0)
and openings (is_open = 1)
for each category per city (e.g. for Korean in Las Vegas the distribution for closings(0) and openings(1) is 50/50).