I have a dataframe from which I'd like to create a list column, and then create a summary table from data in one column of the nested df.
Here's an example:
df <- tribble(
~Location, ~Customer_ID, ~Status, ~Item,
"A", 101, "Elite", "Yoga",
"A", 102, "Standard", "Weights",
"A", 103, "Standard", "Yoga",
"A", 104, "Pro", "Tennis",
"B", 105, "Elite", "Aerobics",
"B", 106, "Pro", "Tennis",
"B", 107, "Pro", "Yoga",
"B", 108, "Standard", "Yoga"
)
I'd like to take this tibble and, first, group by location and nest, so that there's one row per location, with the other three columns nested into a single data column.
Then I'd like to take the data in the Item column for each location, create a count summary from it, and then store that table in a new column, which we can call item_count
. So for Location A, the resulting item_count
table would look like this:
item_count <- tribble(
~Item ~n,
"Yoga", 2,
"Weights", 1,
"Tennis", 1
)
I don't care too much which function is used to create this table; it can be dplyr::count
or janitor::tabyl
or anything similar.
In essence, then, all I need is to map the count()
function onto the Item column in the nested data frame, and save the resulting table in a new column. But I've been stumped as to exactly how to do this.
Any help greatly appreciated.