I have a dataframe:
df = data.frame(temperature = c(rep("1", 3), rep("2", 5), rep("3", 2)), var1 = c(rep("apple", 5), rep("banana", 5)), var2 = c(rep("aardvark", 2), rep("baboon", 4), rep("coyote", 4)), var3 = c(rep("cherry", 7), rep("dodo", 3)))
and want to group by temperature and find the most common factor level across all columns for each temperature as below:
temperature | most common |
---|---|
1 | apple |
2 | dodo |
3 | banana |
I’ve tried to modify solutions from R: Find the most frequent factor level within each group separately for each column and had a play with summarise and across but not having any luck.
Any help greatly appreciated