rdataframefor-loopmeantabulate

Can I calculate the mean/mode of a variable (by another variable) and tabulate in R?


I want to find the mean (for numerical variables) and mode (for character variables) for each of the variables under column "trait_name" for each unique "taxon_name". I would then like to tabulate these values.

This is what my dataframe looks like currently (I also have a wide version):

current dataframe

This is my desired output: goal

I've written code that manually calculates the mean and mode for each "trait_name" per "taxon_name" as follows:

# create mode function
find.mode <- function(x, na.rm = TRUE) {
  
  if(na.rm){ 
    x = x[!is.na(x)]
  }
  
  val <- unique(x)
  return(val[which.max(tabulate(match(x, val)))])
}


# MEAN AND MODE OF TRAITS
## Acacia implexa 
acacia_mass <- mean(species_traits_wide$seed_dry_mass[species_traits_wide$taxon_name == "Acacia implexa"], na.rm = TRUE)
acacia_length <- mean(species_traits_wide$seed_length[species_traits_wide$taxon_name == "Acacia implexa"], na.rm = TRUE)
acacia_form <- find.mode(species_traits_wide$plant_growth_form[species_traits_wide$taxon_name == "Acacia implexa"])
acacia_dormancy <- find.mode(species_traits_wide$seed_dormancy_class[species_traits_wide$taxon_name == "Acacia implexa"])
acacia_treatment <- find.mode(species_traits_wide$seed_germination_treatment[species_traits_wide$taxon_name == "Acacia implexa"])

But I need to automate the process for all species in the dataframe/any new species I wish to add

df <- structure(list(...1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 
93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 
133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 
146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 
159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 
172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 
198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 
211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 
237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 
250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 
263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 
276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 
289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 
302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 
315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 
328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 
341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 
354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 
367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 
380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 
393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 
406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 
419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 
432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 
445, 446, 447, 448, 449, 450, 451), taxon_name = c("Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Acacia implexa", "Acacia implexa", "Acacia implexa", "Acacia implexa", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Casuarina cunninghamiana", "Casuarina cunninghamiana", 
"Casuarina cunninghamiana", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Eucalyptus viminalis", 
"Eucalyptus viminalis", "Eucalyptus viminalis", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Hardenbergia violacea", 
"Hardenbergia violacea", "Hardenbergia violacea", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra", "Themeda triandra", "Themeda triandra", "Themeda triandra", 
"Themeda triandra"), plant_growth_form = c("tree", "shrub", "tree", 
NA, NA, "tree", NA, NA, NA, NA, "tree", NA, NA, "shrub tree", 
NA, "tree", NA, "tree", "tree", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "shrub tree", 
"shrub tree", "tree", "tree", "tree", "tree", "tree", "tree", 
"tree", "tree", NA, NA, NA, NA, "tree", NA, NA, "tree", "tree", 
"tree", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "tree", "tree", "tree", "tree", "tree", 
NA, "tree", "tree", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "tree", NA, "tree", "tree", "tree", "tree", "tree", "tree", 
"tree", "tree", "tree", NA, NA, "climber_herbaceous", "climber", 
"climber", "climber_herbaceous", "climber", NA, NA, NA, NA, NA, 
NA, NA, "climber_herbaceous", NA, NA, "climber", "shrub", NA, 
"subshrub", "shrub", "shrub", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, "subshrub", "subshrub", "climber_woody", "climber_herbaceous herb subshrub", 
"climber", "climber", NA, "climber", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "tussock", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
"graminoid", "tussock", NA, "herb", "graminoid herb", "herb", 
NA, "graminoid herb", "graminoid herb", "graminoid", "herb", 
"graminoid", "graminoid", "graminoid herb", "tussock", "graminoid", 
"tussock", "tussock"), seed_dormancy_class = c(NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, "physical_dormancy", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "non_dormant", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, "physical_dormancy", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), seed_dry_mass = c(NA, 
NA, NA, NA, NA, NA, 13.9712, 17.8, 19.84, 22.86, NA, NA, NA, 
NA, NA, NA, 28.39, NA, NA, NA, 11.14, 10.14, 11.12, 9.92, 9.78, 
11.2, 8.816, 11.012, 8.72, 10.428, 6.088, 9.164, 10.14, 9.876, 
8.632, 9.068, 9.86, 8.328, 8.196, 5, 9.36, 9.104, 9.504, 8.76, 
9.152, 9.036, 8.276, 9.744, 9.296, 10.04, 10.232, 10.616, 8.712, 
9.364, 8.608, 10.736, 6.164, 7.672, 7.98, 6.564, 7.644, 9.552, 
10.344, 10.976, 7.376, 8.924, 8.484, 7.812, 7.172, 8.268, 7.012, 
9.416, 5.904, 10.14, 7.66, 7.92, 9.688, 10.2, 6.008, 8.872, 7.788, 
6.448, 7.416, 10.612, 6.808, 8.4, 7.716, 8.268, 10.12, 7.92, 
7.84, 5.78, 8.956, 7.852, 9.644, 7.296, 7.484, 6.832, 5.752, 
7.064, 10.268, 6.752, 9.276, 4.82, 7.284, 8.304, 7.944, 6.936, 
7.148, 7.864, 8.172, 6.22, 8.372, 8.6, 8.932, 6.592, 6.54, 5.784, 
5.88, 6.164, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.26, 
0.631, 1.59, 3.81, NA, NA, NA, NA, NA, NA, 0.6698, 0.7136, 0.8638, 
0.6398, 0.6592, 0.738, 0.5688, 0.89, 0.8054, 0.5736, 0.9084, 
0.7104, 0.735, 0.7226, 0.6748, 0.742, 0.8002, 0.916, 0.7606, 
0.5774, 0.595, 0.6756, 0.7222, 0.9476, 0.8242, 0.6502, 0.4944, 
0.6392, 0.8068, 0.6578, 0.6546, 0.5928, 0.7562, 0.7618, 0.6378, 
0.7154, 0.8028, 0.3886, 0.7974, 0.6492, 0.5052, 0.7518, 0.7036, 
0.5704, 0.8062, 0.777, 0.7558, 0.6666, 0.6418, 0.848, 0.5736, 
0.3088, 0.7134, 0.5522, 0.7382, 0.9686, 0.3278, 0.718, 0.5976, 
0.5234, 0.4762, 0.456, 0.5732, 0.3338, 0.523, 0.8498, 0.3254, 
0.4948, 0.514, 0.256, 0.5336, 0.3958, 0.5152, 0.4308, 0.409, 
0.552, 0.466, 0.4842, 0.5292, 0.339, 0.2946, 0.4286, 0.2836, 
0.3872, 0.2828, 0.5202, 0.3278, 0.4602, 0.349, 0.3206, 0.471, 
0.2248, 0.3384, 0.49, 0.6982, 0.3084, 0.3388, 0.4416, 0.7154, 
0.8142, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.55, 0.6036, 0.6352, 
0.67, 0.7312, 1.06, 1.1, 2.79, 2.86, 7.83, 150, 173, 600, NA, 
0.731, NA, NA, NA, NA, NA, NA, NA, NA, NA, 22.303, NA, NA, NA, 
NA, NA, NA, NA, 12.371, 12.728, 13.3904, 22, 26.3788, 29.6068, 
NA, NA, NA, NA, NA, 35.03, NA, NA, NA, NA, 25.4, 20, 20.6, 11.4, 
24.8, 18.8, 25.8, 9, 18.2, 19.8, 15.4, 16.7, 17.1, 10.8, 22.6, 
18.1, 15.1, 19.7, 9.5, 8.7, 11.7, 22.4, 15.5, 22.5, 18.4, 16.1, 
15.7, 20.8, 19.7, 19.4, 24.2, 19.4, 23.8, 17, 14.5, 24.9, 14.2, 
21.8, 9.9, 20.1, 20.1, 19.4, 23.6, 18.2, 14, 11.4, 18.7, 14.8, 
10.7, 19.5, 18.6, 19.1, 21, 25, 24, 19, 17.8, 17.2, 22.9, 16.6, 
17.9, 16.5, 13.9, 12.6, 20.6, 15.6, 18.3, 20.6, 18.1, 5, 5.7, 
18.8, 19.9, 18, 19.9, 17.4, 18.1, 22.1, 12.4, 23.5, 18.9, 8.4, 
16, 17.7, 16.2, 19.6, 23.5, 9.6, 10.9, 6.5, NA, NA, NA, NA, NA, 
NA, NA, 39.64, NA, 3.36, 39, 3.2, NA, 4.11, 4.5879, 2.1436, 3.79, 
2.9, NA, 3.85, NA, 1.6, 1.8, 1.975, 2, 2.2, 2.4, 2.8, 3, 3.2, 
3.4, 4, 5.6, 2.4, 2.5, 2.63625, 2.7, 3.136, 3.4544, 4.0384, 4.3856, 
4.5108, 4.7688, 5.1824, 5.25, NA, NA, 2.9, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), seed_germination_treatment = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "heat scarify", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "heat", NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "smoke", NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA), seed_length = c(NA, NA, NA, 
4, 5, NA, NA, NA, NA, NA, NA, 4, 5, NA, 5, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 3, 4, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 1.97, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
5.15, NA, NA, NA, NA, NA, 4.71, NA, NA, NA, NA, NA, NA, NA, 4, 
5, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1.99, 
NA, NA, NA, NA, NA, NA, NA, 16, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA)), row.names = c(NA, -451L), spec = structure(list(cols = list(
    ...1 = structure(list(), class = c("collector_double", "collector"
    )), taxon_name = structure(list(), class = c("collector_character", 
    "collector")), plant_growth_form = structure(list(), class = c("collector_character", 
    "collector")), seed_dormancy_class = structure(list(), class = c("collector_character", 
    "collector")), seed_dry_mass = structure(list(), class = c("collector_double", 
    "collector")), seed_germination_treatment = structure(list(), class = c("collector_character", 
    "collector")), seed_length = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x136a0dac0>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

Solution

  • library(tidyverse)
    
    # mode function is taken from here: https://stackoverflow.com/a/45216553/4145280 - thanks to C8H10N4O2!
    get_mode <- function(x, return_multiple = TRUE, na.rm = FALSE) {
      if(na.rm){
        x <- na.omit(x)
      }
      ux <- unique(x)
      freq <- tabulate(match(x, ux))
      mode_loc <- if(return_multiple) which(freq==max(freq)) else which.max(freq)
      return(ux[mode_loc])
    }
    
    # across all of the columns after taxon_name, check if it's a number column. If yes, get the mean, removing NAs (note: if you don't want to do this, or it's not appropriate for the data, you can remove this). If no, then get the mode (removing NAs too)
    df |> summarise(across(-c(...1), ~ ifelse(is.numeric(.), as.character(mean(., na.rm = TRUE)),get_mode(., na.rm = TRUE))), .by = taxon_name)
    

    Output:

    # A tibble: 5 × 6
      taxon_name               plant_growth_form seed_dormancy_class seed_dry_mass  
      <chr>                    <chr>             <chr>               <chr>          
    1 Acacia implexa           tree              physical_dormancy   8.967897142857…
    2 Casuarina cunninghamiana tree              non_dormant         0.632023076923…
    3 Eucalyptus viminalis     tree              NA                  67.32578571428…
    4 Hardenbergia violacea    climber           physical_dormancy   18.04795959595…
    5 Themeda triandra         tussock           NA                  4.375857352941…
    # ℹ 2 more variables: seed_germination_treatment <chr>, seed_length <chr>