This document is a work in progress, please give us feedback
## [1] 100 7
## [1] 400 2
## species Wood_density Leaf_N SLA Seed_mass
## 1 Macleania rupestris 0.5391685 11.87024 5.23328 2.053831
## 2 Coleostephus myconis 0.7003753 24.43410 20.92570 0.320000
## 3 Guatteria oliviformis 0.5007774 23.54603 16.30982 127.082572
## 4 Myrsine africana 0.6390519 12.90000 11.62415 11.700000
## 5 Chrysophyllum viride 0.6985341 18.14058 12.88736 2000.000000
## 6 Ouratea hexasperma 0.7447541 15.80000 7.29000 147.924327
## Height Leaf_P
## 1 2.4750017 0.7073387
## 2 0.4907508 1.9359780
## 3 16.5535877 1.1148200
## 4 15.2099828 0.8675000
## 5 17.4286478 0.7873025
## 6 7.4563818 0.7630000
## species Biome
## 1 Acacia_podalyriifolia Moist_Forest
## 2 Acer_rubrum Coniferous_Forests
## 3 Acer_rubrum Savannas
## 4 Acer_rubrum Taiga
## 5 Acer_rubrum Temperate_Grasslands
## 6 Acer_rubrum Temperate_Mixed
# How many species are match between the two dataframes
table(sp_traits$species%in%unique(sp_biomes$species))
##
## FALSE
## 100
## [1] Macleania rupestris Coleostephus myconis Guatteria oliviformis
## [4] Myrsine africana Chrysophyllum viride Ouratea hexasperma
## 100 Levels: Acacia podalyriifolia Acer rubrum ... Yucca rostrata
## [1] Acacia_podalyriifolia Acer_rubrum Acer_rubrum
## [4] Acer_rubrum Acer_rubrum Acer_rubrum
## 114 Levels: Abarema_curvicarpa Abies_vejarii ... Yucca_rostrata
# Replacing spaces in species names
sp_traits$species<-gsub(" ","_",sp_traits$species)
head(sp_traits$species)
## [1] "Macleania_rupestris" "Coleostephus_myconis" "Guatteria_oliviformis"
## [4] "Myrsine_africana" "Chrysophyllum_viride" "Ouratea_hexasperma"
##
## FALSE TRUE
## 1 99
##
## FALSE TRUE
## 15 99
# ?merge
# When species columns have the same name
Trait_biomes_df<-merge(x=sp_traits,y=sp_biomes)
head(Trait_biomes_df)
## species Wood_density Leaf_N SLA Seed_mass Height
## 1 Acacia_podalyriifolia 0.68 26.72163 6.344215 32.65056 23.80193
## 2 Acer_rubrum 0.49 16.94147 20.501333 22.76456 36.99420
## 3 Acer_rubrum 0.49 16.94147 20.501333 22.76456 36.99420
## 4 Acer_rubrum 0.49 16.94147 20.501333 22.76456 36.99420
## 5 Acer_rubrum 0.49 16.94147 20.501333 22.76456 36.99420
## 6 Acer_rubrum 0.49 16.94147 20.501333 22.76456 36.99420
## Leaf_P Biome
## 1 0.7857099 Moist_Forest
## 2 1.1150000 Temperate_Grasslands
## 3 1.1150000 Coniferous_Forests
## 4 1.1150000 Taiga
## 5 1.1150000 Temperate_Mixed
## 6 1.1150000 Savannas
DF1<-merge(x=sp_traits,y=sp_biomes,
by.x="species",by.y="species")
DF2<-merge(x=sp_biomes,y=sp_traits,
by.x="species",by.y="species")
Is there a difference between data frame DF1
and DF2
?
Include the Growth form information into the Trait_biomes_df
data frame. Remember to check first the format and names of the variables you want to match between the two data frames.
Let’s recap the main structure of a function in R!
## [1] "function (x, y = 1) " "{" " x + y"
## [4] "}"
## {
## x + y
## }
## <environment: R_GlobalEnv>
## [1] 3
## [1] 10
Extracting the mean values of seed mass per family in Moist forest
Trait_biomes_df %>%
filter(Biome=="Moist_Forest") %>%
group_by(FAMILY_STD) %>%
summarise(Mean_Trait=mean(Seed_mass, na.rm=TRUE))
## # A tibble: 28 x 2
## FAMILY_STD Mean_Trait
## <fct> <dbl>
## 1 Amaranthaceae 2.71
## 2 Anacardiaceae 13.0
## 3 Annonaceae 2242.
## 4 Apiaceae 0.731
## 5 Bignoniaceae 14.1
## 6 Brassicaceae 0.339
## 7 Burseraceae 269.
## 8 Caricaceae 6.00
## 9 Chrysobalanaceae 2425.
## 10 Cyperaceae 0.0210
## # ... with 18 more rows
Create a function that takes any biome name as input and display the average value of seed mass per family
Modify the function to calculate the average value of any trait
Complete the ----
in the following code to create a function that: