Home > OS >  Produce table output for summary statistic
Produce table output for summary statistic

Time:03-14

I'm running a loop to retrieve the mode of a column in R according to a condition. Once the mode is calculated, I would like to append it to a matrix (or data frame) with the name of the condition attached.

In my attempt, however, it assigns the word 'label' to the resulting mode, i.e., label = 9. How would I correct this such that instead of saying, e.g., label = 9, it says "blues" = 9, with "blues" being the name of the label in this case?

clusters <- NULL
# Loop over each genre label, return mode for each
for (label in unique(music$label)) {
  mode <- getmode(music$cluster[music$label == label])
  clusters <- rbind(clusters, label = mode)
}

CodePudding user response:

As mentioned by @BillO'Brien, base R's aggregate can work as well:

aggregate(cbind(mode=cluster) ~ label, music, FUN=getmode)

CodePudding user response:

Solved without using a loop as per @bill-obrien's comment. Using 'group_by' in tidyverse to group by the unique labels in the column, and then for each unique label, found the statistical mode of the second column ('cluster'), which is returned in a tibble - a data frame format.

music %>% group_by(label) %>%
  summarise(mode = getmode(music$cluster[music$label == label]))
  • Related