Extract species x traits information from long format data
Source:R/fb_format_species_traits.R
fb_format_species_traits.Rd
Convert a flat data.frame
with traits values for different species
into a proper data.frame
object that can then be used by other functions.
The final output contains species in rows and traits in columns.
Value
A data.frame
with species in rows and traits in columns, with the
first column names "species"
containing the species names.
Examples
library("funbiogeo")
filename <- system.file("extdata", "raw_mammals_data.csv",
package = "funbiogeo")
all_data <- read.csv(filename)
head(all_data)
#> species order site longitude latitude count adult_body_mass
#> 1 sp_001 Cetartiodactyla fb_103 7.271821 59.09736 1 461900.8
#> 2 sp_001 Cetartiodactyla fb_1001 20.771821 52.59736 1 461900.8
#> 3 sp_001 Cetartiodactyla fb_102 6.771821 59.09736 1 461900.8
#> 4 sp_001 Cetartiodactyla fb_104 7.771821 59.09736 1 461900.8
#> 5 sp_001 Cetartiodactyla fb_101 6.271821 59.09736 1 461900.8
#> 6 sp_001 Cetartiodactyla fb_1000 20.271821 52.59736 1 461900.8
#> gestation_length litter_size max_longevity sexual_maturity_age diet_breadth
#> 1 235 1.25 324 668.2 1
#> 2 235 1.25 324 668.2 1
#> 3 235 1.25 324 668.2 1
#> 4 235 1.25 324 668.2 1
#> 5 235 1.25 324 668.2 1
#> 6 235 1.25 324 668.2 1
traits <- c("adult_body_mass", "gestation_length", "litter_size",
"max_longevity", "sexual_maturity_age", "diet_breadth")
species_traits <- fb_format_species_traits(all_data, "species", traits)
head(species_traits)
#> species adult_body_mass gestation_length litter_size max_longevity
#> 1 sp_001 461900.76 235.00 1.25 324.0
#> 2 sp_002 21.11 19.89 5.64 48.0
#> 3 sp_005 31.60 24.50 4.94 48.0
#> 4 sp_006 21.90 23.68 5.16 52.8
#> 5 sp_010 8.31 NA 1.73 252.0
#> 6 sp_013 31756.51 63.50 4.98 354.0
#> sexual_maturity_age diet_breadth
#> 1 668.20 1
#> 2 76.04 NA
#> 3 43.27 NA
#> 4 57.93 4
#> 5 NA 1
#> 6 679.37 1