I try to subset a table based on one category value. Assume we want to keep only adults from the Titanic data. What I do is:
data("Titanic")
subset(Titanic, Age == "Adult")
This results in the error object 'Age' not found
. Using same logic with dataframes works: subset(as.data.frame(Titanic), Age == "Adult")
. But how can we subset tables directly, i.e. without transforming them to a dataframe?
EDIT
Here Adult
is dimension number three. In my case I do not know which dimension it is, i.e. I would like to be able to subset by variable name as in subset(Titanic, Age == "Adult")
. It can be any other base function, i.e. I am not stuck with subset
. But I am looking for a base R solution.
My expected output is
structure(c(118, 154, 387, 670, 4, 13, 89, 3, 57, 14, 75, 192, 140, 80, 76, 20), .Dim = c(4L, 2L, 2L), .Dimnames = list(Class = c("1st", "2nd", "3rd", "Crew"), Sex = c("Male", "Female"), Survived = c("No", "Yes")), class = "table")