I want to add up the number of times a value appears based on another value. Example data:
df <- data.frame(hour = c("1", "2", "1", "2", "3", "2", "3"), name = c("A", "B", "A", "B", "C", "A", "B"))
Using table (table(df$hour, df$name
) gives me exactly the right output but I don't want a table - I want to do a heat map in ggplot and need a dataframe. I have been pulling my hair out - there has to be an easy way.
The table output can be converted to data frame. Use one of these depending on the output desired:
heatmap
Regarding heatmaps note that
heatmap
in the base of R acceptstable
output directly (and also gplots::balloonplot not shown here accepts table output):It can also be done in ggpubr::balloonplot, lattice::levelplot or ggplot2 using
as.data.frame(table(df))
:The output looks like this (see Note at end for code that generated this):
Note