Starting code for importing cliff data
Georgia Harrison, March 2021
This code is written to streamline the process of importing cliff survey data (or any other ecological survey). We will import the data, get it into the right format for vegan, and summarize species richness, diversity and cover values
install packages
I like to keep a running list of all the packages I use at the top of my code like this. Run install.packages() in console. In the script, just library() so that the packages do not get reloaded a bunch of times. Once it is installed once you should be good to go until you update your R
library(vegan) #community ecology analysis
library(ggplot2) #data viz
library(dplyr) #data manipulation
library(tidyr) #data manipulation
library(ddpcr) #for merging data framesdata import
import the data file - the current format of the data is long, which should be the way you enter the data with long data format, each species observation in each plot will have its own row (and accompanying metadata and abiotic information).
Important to check before you import this data:
each species code is unique
consistent labeling (i.e. if you have all caps for climbed or unclimbed column, make sure it is the same for the whole data file)
no spaces in the column labels which you care about or will be using for analysis.
I might have a “notes” column that is just for my own sake and will not be used for analysis, so I don’t need to worry about it. It
does matter for things you will be using, like route name. Just use underscores instead of spaces
format should be .csv file
In this case I am only importing one file so it is easy to keep track of what is going on, but if I bring in more files I like to give a short one or two line description of the data file so it would make sense to me in a year or two.
long <- read.csv("~/Cliffs/Keeha data analysis/practice_cliff_long.csv", header=TRUE)let’s take a look at this data to get an idea of what we are working with
head(long) #head will show first 6 lines## DATE ROUTE PLOT_NUM P_LOCATION CL_UNCL T_LOCATION GRADE_CAT GRADE
## 1 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## 2 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## 3 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## 4 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## 5 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## 6 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## STARS ROUTE_TYPE TOT_HEIGHT ASPECT_ORD ASPECT_DEG ASPECT_NORTH ASPECT_EAST
## 1 2.3 T 32 E 155 -0.91 0.42
## 2 2.3 T 32 E 155 -0.91 0.42
## 3 2.3 T 32 E 155 -0.91 0.42
## 4 2.3 T 32 E 155 -0.91 0.42
## 5 2.3 T 32 E 155 -0.91 0.42
## 6 2.3 T 32 E 155 -0.91 0.42
## SLOPE HEIGHT_DOWN TAXA Functional Species Abundance
## 1 130 3 L CRUST BLBRDOT 9
## 2 130 3 L CRUST MINGREY 9
## 3 130 3 L CRUST GRNCRST 1
## 4 130 3 L LOBE TNYBRNCH 9
## 5 130 3 L LOBE GRNSQB 6
## 6 130 3 L POWDER GRNPDR 3
tail(long) #tail will show last 6 lines## DATE ROUTE PLOT_NUM P_LOCATION CL_UNCL T_LOCATION GRADE_CAT
## 2820 4-Jun-19 WOOLY APHID 14 FACE CLIMBED TR ADVANCED
## 2821 4-Jun-19 WOOLY APHID 14 FACE CLIMBED TR ADVANCED
## 2822 4-Jun-19 WOOLY APHID 14 FACE CLIMBED TR ADVANCED
## 2823 4-Jun-19 WOOLY APHID 14 FACE CLIMBED TR ADVANCED
## 2824 4-Jun-19 WOOLY APHID 1B BASE CLIMBED TR ADVANCED
## 2825 4-Jun-19 WOOLY APHID 2B BASE CLIMBED TR ADVANCED
## GRADE STARS ROUTE_TYPE TOT_HEIGHT ASPECT_ORD ASPECT_DEG ASPECT_NORTH
## 2820 5.10a 2.5 T 40 E 120 -0.5
## 2821 5.10a 2.5 T 40 E 120 -0.5
## 2822 5.10a 2.5 T 40 E 120 -0.5
## 2823 5.10a 2.5 T 40 E 120 -0.5
## 2824 5.10a 2.5 T 40 E 120 -0.5
## 2825 5.10a 2.5 T 40 E 120 -0.5
## ASPECT_EAST SLOPE HEIGHT_DOWN TAXA Functional Species Abundance
## 2820 0.87 105 32 L POWDER MINTPDR 3
## 2821 0.87 105 32 L POWDER WHPDR 2
## 2822 0.87 105 32 L POWDER GRNPDR 1
## 2823 0.87 105 32 L UMB TOADSK 2
## 2824 0.87 NA NA P Car_3 6
## 2825 0.87 NA NA P Car_3 2
Description of the columns:
- DATE is survey date
- ROUTE is the name of the climbing route or number of unclimbed transect
- PLOT_NUM is
- P_LOCATION is plot location, either top, base or face
- CL_UNCL is if the plot was UNCLIMBED or CLIMBED
- T_LOCATION is site (i this case, it is either TR for Table Rock or HB for hawksbill)
- GRADE_CAT is category for climbing grade, either EASY, MODERATE, or ADVANCED for climbing routes. UNCLIMBED for unclimbed routes
- GRADE is YDS grade for climbing routes - blank for unclimbed
- STARS is the star rating for climbing route from local guide book or mountain project
- ROUTE_TYPE is either traditional (T) or mixed (T_S)
- TOT_HEIGHT is total height of the survey transect in meters
- ASPECT_ORD is compass direction for aspect (aspect was taken for entire transect)
- ASPECT_DEG is compass direction transformed to degrees
- ASPECT_NORTH is aspect into linear components (north-south component)
- ASPECT_EAST is aspect into linear components (east-west component)
- SLOPE is slope in degrees from center of each plot
- HEIGHT_DOWN is meters down survey transect for each plot
- TAXA taxa group for each species, either L = lichen, M = bryophyte, P = vascular plant
- Functional is functional group for lichens, either CRUST, POWDER, FRUIT, UMB, or LOBE
- Species is species code or identifier. Keep a seperate data set with the species code and what they actually mean
- Abundance field recorded abundance value, a value from 0-9 since my plots were subsetted into 9 smaller plots. I recorded presence / absence within each microplot. I am using this as a proxy for cover
As you can see, some of these values will be important for analysis and some will not. We will subset out the unimportant ones later.
If you want to take a closer look at one column, you can use summary function with dollar sign. The dollar sign is referring to a column in the data frame such that data$column.
summary(long$GRADE_CAT)## Length Class Mode
## 2825 character character
Tranforming field abundance to cover values that have more meaning
If you recorded cover in the field you can skip this step.
Abundance value of * 9 = 100% cover * 8 = (8/9) * 100 % cover * 7 = (7/0) * 100 % cover and so on…
First, figure out which column in your data set it abundance since you only want to change that one.
which(colnames(long)=="Abundance") #21## [1] 21
We are telling R to look in the long dataframe in column 21 for values of 9 and to replace it with 100. I am using number 21 bc I am telling R that is the only column I was it to mess with. I am using R like a calculator to input the correct values .
long[,21][long[,21]== 9] <- 100
long[,21][long[,21]== 8] <- (8/9)*100
long[,21][long[,21]== 7] <- (7/9)*100
long[,21][long[,21]== 6] <- (6/9)*100
long[,21][long[,21]== 5] <- (5/9)*100
long[,21][long[,21]== 4] <- (4/9)*100
long[,21][long[,21]== 3] <- (3/9)*100
long[,21][long[,21]== 2] <- (2/9)*100
long[,21][long[,21]== 1] <- (1/9)*100Same process would work if you had recorded cover in cover classes and you need to sub in midpoint values
Subset data by taxa groups
In the case of this data, I am interested in summarizing species richness, diversity and cover for all taxa but then also for each taxa group (lichen, bryos and plants), so I need to create subsets of the long data that include only a certain taxa group.
lichen <- long[long$TAXA=="L", ]
moss <- long[long$TAXA=="M", ]
plant <- long[long$TAXA=="P", ]Transform data from long to wide format
here had good info about tidy data and transforming from wide to long format or vise versa wide = each plot has its own row
library(tidyr) # this is to remind myself what package I am using
wide <- spread(long, Species, Abundance)
#long is the data set which you would like to transform
#Species is the column that you want broken up into a bunch of columns
#abundance is the value to insert into each species columnDo the same thing for the subsetted data
lichen_wide <- spread(lichen, Species, Abundance)
moss_wide <- spread(moss, Species, Abundance)
plant_wide <- spread(plant, Species, Abundance)Now that you have the data in matrix form, you can calculate species richness and diversity
Vegan reads data as a species abundance matrix where each columns in a species and each plot is row. here is a good tutorial if you need some extra help with vegan. You need to figure out which species are the first and last columns so you can select those and make a subset of just species data. Your species columns will be in alphabetical order (just the nature of how it is tranformed from long to wide). I use the head() function to figure out which species is first and last. The number after head tells R how many rows you want to see, in this case I only need the column names so just 1 row is fine. Once you figure out which column section are the species, you can subset it out to make an abundance matrix.
I will walk through step by step for the all taxa data set
head(wide, 1)## DATE ROUTE PLOT_NUM P_LOCATION CL_UNCL T_LOCATION GRADE_CAT GRADE
## 1 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## STARS ROUTE_TYPE TOT_HEIGHT ASPECT_ORD ASPECT_DEG ASPECT_NORTH ASPECT_EAST
## 1 2.3 T 32 E 155 -0.91 0.42
## SLOPE HEIGHT_DOWN TAXA Functional Ace_saccharum Agr_parennans And_rothii
## 1 130 3 L CRUST NA NA NA
## And_virginicus.var.virginicus Atr_angustatum Bet_alleghaniensis BLAFLA BLASM
## 1 NA NA NA NA NA
## BLASQ BLAWHT BLBRDOT BLGRDOT BLKPDR BLWHDOT BRBLDOT BRBLKBDR BRBLKCRST
## 1 NA NA 100 NA NA NA NA NA NA
## BRGCRUST BRNFOL BRNGRCRST BRNGRFOL BRNWART BRNYELL BROPDR Bry_sp BUBLGUM
## 1 NA NA NA NA NA NA NA NA NA
## Buc_venusta Cam_tallulensis Car_2 Car_3 Car_4 Car_5 Car_6 Car_7 Car_umbellata
## 1 NA NA NA NA NA NA NA NA NA
## Cer_purpureus CLAD CLADBRS CLADBSTK CLADPIX CLADRC CLADSQ CLADSTLK Cor_major
## 1 NA NA NA NA NA NA NA NA NA
## Den_punctilobula Dic_accumulatum Dic_heteromalla Dic_montanum Dic_scoparium
## 1 NA NA NA NA NA
## Dic_varia Dip_apiculatum Dit_lineare Dit_pusillum Gal_urceolata Graminoid_1
## 1 NA NA NA NA NA NA
## GRBBRD GREEREIN GREEWART GREMED GREPAPER GRESMSQ GREYBLDOT GREYPDR GREYREIN
## 1 NA NA NA NA NA NA NA NA NA
## GRMED GRMEDSQ GRNCIL GRNCRST GRNFOL GRNPDR GRNSM GRNSQB GRNWART GRNWDE
## 1 NA NA NA 11.11111 NA NA NA NA NA NA
## GRWIDE GRYBR GRYBRN Hyd_petiolaris JETBLK Kal_buxifolia Kal_latifolia
## 1 NA NA NA NA NA NA NA
## Leu_albidum Leu_glaucum MINGREY MINTPDR Moss_10 Moss_11 Moss_12 Moss_13
## 1 NA NA 100 NA NA NA NA NA
## Moss_14 Moss_15 Moss_16 Moss_2 Moss_4 Moss_5 Moss_6 Moss_7 Moss_8 Moss_9
## 1 NA NA NA NA NA NA NA NA NA NA
## Nys_sylvatica Oxy_arboreum PAPER Pin_rigida PNKBLA PNKCRST Poc_juniperinum
## 1 NA NA NA NA NA NA NA
## Poh_nutans Pol_commune Pol_juniperinum Pol_piliferum Pol_strictum Pse_elegans
## 1 NA NA NA NA NA NA
## PSYCHO Rac_heterostichum RAMALI REDCRST Rho_major Rho_minus ROCKTRP
## 1 NA NA NA NA NA NA NA
## Rub_allegheniensis Sel_tortipila SHIELD SOILCR TANBUB TANCUP TEALCRST
## 1 NA NA NA NA NA NA NA
## TNYBRNCH TOADGR TOADSK USNEA Wei_controversa WHBLDOT WHITBUB WHITEFOL
## 1 NA NA NA NA NA NA NA NA
## WHITREIN WHPDR YELLWART YELPDR
## 1 NA NA NA NA
Okay from this, I see that Ace_saccharum is the first species and YELPDR is the last. Now lets figure out which column numbers these are. Spelling and capitalization matters.
which(colnames(wide)=="Ace_saccharum") #20## [1] 20
which( colnames(wide)=="YELPDR") #157## [1] 157
Output is column 20 and 157, which I write down to remember.
Now create an abundance matrix using the column numbers we got from above.
total.abundance.matrix <- wide[,20:157]We need to replace na values with 0 so that vegan can read it later on
total.abundance.matrix[is.na(total.abundance.matrix)] =0If we take a quick peak at the abundance matrix, you can see it is only species and none of the plot info. Now it is ready to go into vegan
head(total.abundance.matrix)## Ace_saccharum Agr_parennans And_rothii And_virginicus.var.virginicus
## 1 0 0 0 0
## 2 0 0 0 0
## 3 0 0 0 0
## 4 0 0 0 0
## 5 0 0 0 0
## 6 0 0 0 0
## Atr_angustatum Bet_alleghaniensis BLAFLA BLASM BLASQ BLAWHT BLBRDOT BLGRDOT
## 1 0 0 0 0 0 0 100.00000 0
## 2 0 0 0 0 0 0 0.00000 0
## 3 0 0 0 0 0 0 0.00000 0
## 4 0 0 0 0 0 0 0.00000 0
## 5 0 0 0 0 0 0 0.00000 0
## 6 0 0 0 0 0 0 55.55556 0
## BLKPDR BLWHDOT BRBLDOT BRBLKBDR BRBLKCRST BRGCRUST BRNFOL BRNGRCRST BRNGRFOL
## 1 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0
## BRNWART BRNYELL BROPDR Bry_sp BUBLGUM Buc_venusta Cam_tallulensis Car_2 Car_3
## 1 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0
## Car_4 Car_5 Car_6 Car_7 Car_umbellata Cer_purpureus CLAD CLADBRS CLADBSTK
## 1 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0
## CLADPIX CLADRC CLADSQ CLADSTLK Cor_major Den_punctilobula Dic_accumulatum
## 1 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0
## Dic_heteromalla Dic_montanum Dic_scoparium Dic_varia Dip_apiculatum
## 1 0 0 0 0 0
## 2 0 0 0 0 0
## 3 0 0 0 0 0
## 4 0 0 0 0 0
## 5 0 0 0 0 0
## 6 0 0 0 0 0
## Dit_lineare Dit_pusillum Gal_urceolata Graminoid_1 GRBBRD GREEREIN GREEWART
## 1 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0
## GREMED GREPAPER GRESMSQ GREYBLDOT GREYPDR GREYREIN GRMED GRMEDSQ GRNCIL
## 1 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0
## GRNCRST GRNFOL GRNPDR GRNSM GRNSQB GRNWART GRNWDE GRWIDE GRYBR GRYBRN
## 1 11.11111 0 0.00000 0 0.00000 0 0 0 0 0
## 2 0.00000 0 0.00000 0 66.66667 0 0 0 0 0
## 3 0.00000 0 33.33333 0 0.00000 0 0 0 0 0
## 4 0.00000 0 0.00000 0 0.00000 0 0 0 0 0
## 5 0.00000 0 0.00000 0 0.00000 0 0 0 0 0
## 6 22.22222 0 0.00000 0 0.00000 0 0 0 0 0
## Hyd_petiolaris JETBLK Kal_buxifolia Kal_latifolia Leu_albidum Leu_glaucum
## 1 0 0 0 0 0 0
## 2 0 0 0 0 0 0
## 3 0 0 0 0 0 0
## 4 0 0 0 0 0 0
## 5 0 0 0 0 0 0
## 6 0 0 0 0 0 0
## MINGREY MINTPDR Moss_10 Moss_11 Moss_12 Moss_13 Moss_14 Moss_15 Moss_16
## 1 100 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## 6 100 0 0 0 0 0 0 0 0
## Moss_2 Moss_4 Moss_5 Moss_6 Moss_7 Moss_8 Moss_9 Nys_sylvatica Oxy_arboreum
## 1 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0
## PAPER Pin_rigida PNKBLA PNKCRST Poc_juniperinum Poh_nutans Pol_commune
## 1 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0
## Pol_juniperinum Pol_piliferum Pol_strictum Pse_elegans PSYCHO
## 1 0 0 0 0 0
## 2 0 0 0 0 0
## 3 0 0 0 0 0
## 4 0 0 0 0 0
## 5 0 0 0 0 0
## 6 0 0 0 0 0
## Rac_heterostichum RAMALI REDCRST Rho_major Rho_minus ROCKTRP
## 1 0 0 0 0 0 0.00000
## 2 0 0 0 0 0 0.00000
## 3 0 0 0 0 0 0.00000
## 4 0 0 0 0 0 11.11111
## 5 0 0 0 0 0 0.00000
## 6 0 0 0 0 0 0.00000
## Rub_allegheniensis Sel_tortipila SHIELD SOILCR TANBUB TANCUP TEALCRST
## 1 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0
## TNYBRNCH TOADGR TOADSK USNEA Wei_controversa WHBLDOT WHITBUB WHITEFOL
## 1 0 0 0.00000 0 0.00000 0 0 0
## 2 100 0 0.00000 0 0.00000 0 0 0
## 3 0 0 0.00000 0 0.00000 0 0 0
## 4 0 0 77.77778 0 0.00000 0 0 0
## 5 0 0 0.00000 0 22.22222 0 0 0
## 6 0 0 0.00000 0 0.00000 0 0 0
## WHITREIN WHPDR YELLWART YELPDR
## 1 0 0.00000 0 0
## 2 0 0.00000 0 0
## 3 0 33.33333 0 0
## 4 0 0.00000 0 0
## 5 0 0.00000 0 0
## 6 0 0.00000 0 0
let’s do this for the rest of the taxa groups
lichen
head(lichen_wide, 1)## DATE ROUTE PLOT_NUM P_LOCATION CL_UNCL T_LOCATION GRADE_CAT GRADE
## 1 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## STARS ROUTE_TYPE TOT_HEIGHT ASPECT_ORD ASPECT_DEG ASPECT_NORTH ASPECT_EAST
## 1 2.3 T 32 E 155 -0.91 0.42
## SLOPE HEIGHT_DOWN TAXA Functional BLAFLA BLASM BLASQ BLAWHT BLBRDOT BLGRDOT
## 1 130 3 L CRUST NA NA NA NA 100 NA
## BLKPDR BLWHDOT BRBLDOT BRBLKBDR BRBLKCRST BRGCRUST BRNFOL BRNGRCRST BRNGRFOL
## 1 NA NA NA NA NA NA NA NA NA
## BRNWART BRNYELL BROPDR BUBLGUM CLAD CLADBRS CLADBSTK CLADPIX CLADRC CLADSQ
## 1 NA NA NA NA NA NA NA NA NA NA
## CLADSTLK GRBBRD GREEREIN GREEWART GREMED GREPAPER GRESMSQ GREYBLDOT GREYPDR
## 1 NA NA NA NA NA NA NA NA NA
## GREYREIN GRMED GRMEDSQ GRNCIL GRNCRST GRNFOL GRNPDR GRNSM GRNSQB GRNWART
## 1 NA NA NA NA 11.11111 NA NA NA NA NA
## GRNWDE GRWIDE GRYBR GRYBRN JETBLK MINGREY MINTPDR PAPER PNKBLA PNKCRST PSYCHO
## 1 NA NA NA NA NA 100 NA NA NA NA NA
## RAMALI REDCRST ROCKTRP SHIELD SOILCR TANBUB TANCUP TEALCRST TNYBRNCH TOADGR
## 1 NA NA NA NA NA NA NA NA NA NA
## TOADSK USNEA WHBLDOT WHITBUB WHITEFOL WHITREIN WHPDR YELLWART YELPDR
## 1 NA NA NA NA NA NA NA NA NA
which( colnames(lichen_wide)=="BLAFLA") #20## [1] 20
which( colnames(lichen_wide)=="YELPDR") #93## [1] 93
lichen.abundance.matrix <- lichen_wide[,20:93]
lichen.abundance.matrix[is.na(lichen.abundance.matrix)] =0mosses
head(moss_wide, 1)## DATE ROUTE PLOT_NUM P_LOCATION CL_UNCL T_LOCATION GRADE_CAT GRADE
## 1 28-May-19 BLOCK ROUTE 1 FACE CLIMBED TR EASY 5.5
## STARS ROUTE_TYPE TOT_HEIGHT ASPECT_ORD ASPECT_DEG ASPECT_NORTH ASPECT_EAST
## 1 2.3 T 32 E 155 -0.91 0.42
## SLOPE HEIGHT_DOWN TAXA Functional And_rothii Atr_angustatum Bry_sp
## 1 130 3 M NA NA NA
## Buc_venusta Cam_tallulensis Cer_purpureus Dic_heteromalla Dic_montanum
## 1 NA NA NA NA NA
## Dic_scoparium Dic_varia Dip_apiculatum Dit_lineare Dit_pusillum Leu_albidum
## 1 NA NA NA NA NA NA
## Leu_glaucum Moss_10 Moss_11 Moss_12 Moss_13 Moss_14 Moss_15 Moss_16 Moss_2
## 1 NA NA NA NA NA NA NA NA NA
## Moss_4 Moss_5 Moss_6 Moss_7 Moss_8 Moss_9 Poc_juniperinum Poh_nutans
## 1 NA NA NA NA NA NA NA NA
## Pol_commune Pol_juniperinum Pol_piliferum Pol_strictum Pse_elegans
## 1 NA NA NA NA NA
## Rac_heterostichum Wei_controversa
## 1 NA 22.22222
which( colnames(moss_wide)=="And_rothii")#20## [1] 20
which( colnames(moss_wide)=="Wei_controversa")#57## [1] 57
moss.abundance.matrix <- moss_wide[,20:57]
moss.abundance.matrix[is.na(moss.abundance.matrix)] =0plants
head(plant_wide, 1)## DATE ROUTE PLOT_NUM P_LOCATION CL_UNCL T_LOCATION GRADE_CAT GRADE
## 1 28-May-19 BLOCK ROUTE 2 FACE CLIMBED TR EASY 5.5
## STARS ROUTE_TYPE TOT_HEIGHT ASPECT_ORD ASPECT_DEG ASPECT_NORTH ASPECT_EAST
## 1 2.3 T 32 E 155 -0.91 0.42
## SLOPE HEIGHT_DOWN TAXA Functional Ace_saccharum Agr_parennans
## 1 130 3 P NA NA
## And_virginicus.var.virginicus Bet_alleghaniensis Car_2 Car_3 Car_4 Car_5
## 1 NA NA NA NA NA NA
## Car_6 Car_7 Car_umbellata Cor_major Den_punctilobula Dic_accumulatum
## 1 NA NA NA NA NA NA
## Gal_urceolata Graminoid_1 Hyd_petiolaris Kal_buxifolia Kal_latifolia
## 1 NA NA NA NA NA
## Nys_sylvatica Oxy_arboreum Pin_rigida Rho_major Rho_minus Rub_allegheniensis
## 1 NA NA NA NA NA NA
## Sel_tortipila
## 1 22.22222
which( colnames(plant_wide)=="Ace_saccharum")#20## [1] 20
which( colnames(plant_wide)=="Sel_tortipila")#45## [1] 45
plant.abundance.matrix <- plant_wide[,20:45]
plant.abundance.matrix[is.na(plant.abundance.matrix)] =0Actually calculating species richness, diversity and cover.
For each taxa group, you need to create a new data frame where you can insert your richness n such outputs. But you still want the info about each plot (was it climbed or unclmbed?), so bring in the columns from OG data which describe plot. Note that I only included factors which I think will be important for analysis (i.e. not YDS grade or height down).
ALL TAXA richness, diversity and cover
i_wide <- wide[,c("CL_UNCL", "PLOT_NUM", "P_LOCATION", "T_LOCATION", "GRADE_CAT",
"TOT_HEIGHT", "SLOPE", "ASPECT_NORTH", "ASPECT_EAST")]
i_wide$AllRichness <- rowSums(total.abundance.matrix>0) #sp richness is a count of how many species are present in a plot
i_wide$AllShannon <- diversity(total.abundance.matrix) # shannon is default
i_wide$AllSimpson <- diversity(total.abundance.matrix, index = "simpson") #simpson diversity index if you wanted that
i_wide$AllCover <- rowSums(total.abundance.matrix) #this is summing all of the cover values for each row, remember that each row is a plotLichen richness, diversity and cover.
i_lichen <- lichen_wide[,c("CL_UNCL", "PLOT_NUM", "P_LOCATION", "T_LOCATION", "GRADE_CAT",
"TOT_HEIGHT", "SLOPE", "ASPECT_NORTH", "ASPECT_EAST")]
i_lichen$LRichness <- rowSums(lichen.abundance.matrix>0)
i_lichen$LShannon <- diversity(lichen.abundance.matrix) # shannon is default
i_lichen$LSimpson <- diversity(lichen.abundance.matrix, index = "simpson")
i_lichen$LCover <- rowSums(lichen.abundance.matrix)Combine all of those data sets into one for analysis. The merge_dfs_overwrite_col function is from package ddpcr, you input the two data sets you would like to combine (in this case it is the all taxa group summary and the lichen summary) and tell it which col you would like to merge it by. It will overwrite any of the bycol ones so you only have one of each column, it makes a nice output good for future analysis.
indic<-
merge_dfs_overwrite_col(
i_wide, i_lichen,
bycol = c("CL_UNCL", "PLOT_NUM", "P_LOCATION", "T_LOCATION",
"GRADE_CAT", "TOT_HEIGHT", "SLOPE", "ASPECT_NORTH",
"ASPECT_EAST")
)
head(indic)## CL_UNCL PLOT_NUM P_LOCATION T_LOCATION GRADE_CAT TOT_HEIGHT SLOPE
## 1 CLIMBED 1 FACE TR EASY 32 130
## 2 CLIMBED 1 FACE TR EASY 32 130
## 3 CLIMBED 1 FACE TR EASY 32 130
## 4 CLIMBED 1 FACE TR EASY 32 130
## 5 CLIMBED 1 FACE TR EASY 32 130
## 6 CLIMBED 1 FACE TR EASY 32 130
## ASPECT_NORTH ASPECT_EAST AllRichness AllShannon AllSimpson AllCover LRichness
## 1 -0.91 0.42 3 0.8628578 0.5484765 211.1111 3
## 2 -0.91 0.42 3 0.8628578 0.5484765 211.1111 2
## 3 -0.91 0.42 3 0.8628578 0.5484765 211.1111 2
## 4 -0.91 0.42 3 0.8628578 0.5484765 211.1111 2
## 5 -0.91 0.42 2 0.6730117 0.4800000 166.6667 3
## 6 -0.91 0.42 2 0.6730117 0.4800000 166.6667 2
## LShannon LSimpson LCover
## 1 0.8628578 0.5484765 211.11111
## 2 0.6730117 0.4800000 166.66667
## 3 0.6931472 0.5000000 66.66667
## 4 0.3767702 0.2187500 88.88889
## 5 0.8628578 0.5484765 211.11111
## 6 0.6730117 0.4800000 166.66667
Now lets caluclate moss and plant richness, diversity and cover.
Moss richness, diversity and cover
i_moss<- moss_wide[,c("CL_UNCL", "PLOT_NUM", "P_LOCATION", "T_LOCATION", "GRADE_CAT",
"TOT_HEIGHT", "SLOPE", "ASPECT_NORTH", "ASPECT_EAST")]
i_moss$MRichness <- rowSums(moss.abundance.matrix>0)
i_moss$MShannon <- diversity(moss.abundance.matrix) # shannon is default
i_moss$MSimpson <- diversity(moss.abundance.matrix, index = "simpson")
i_moss$MCover <- rowSums(moss.abundance.matrix)Plant richness, diversity and cover
i_plant <- plant_wide[,c("CL_UNCL", "PLOT_NUM", "P_LOCATION", "T_LOCATION", "GRADE_CAT",
"TOT_HEIGHT", "SLOPE", "ASPECT_NORTH", "ASPECT_EAST")]
i_plant$PRichness <- rowSums(plant.abundance.matrix>0)
i_plant$PShannon <- diversity(plant.abundance.matrix) # shannon is default
i_plant$PSimpson <- diversity(plant.abundance.matrix, index = "simpson")
i_plant$PCover <- rowSums(plant.abundance.matrix)combine plant and moss with previously combined data (all taxa + lichen)
indic<-
merge_dfs_overwrite_col(
indic, i_moss,
bycol = c("CL_UNCL", "PLOT_NUM", "P_LOCATION",
"T_LOCATION", "GRADE_CAT", "TOT_HEIGHT",
"SLOPE", "ASPECT_NORTH", "ASPECT_EAST")
)
indic<-
merge_dfs_overwrite_col(
indic, i_plant,
bycol = c("CL_UNCL", "PLOT_NUM", "P_LOCATION",
"T_LOCATION", "GRADE_CAT", "TOT_HEIGHT",
"SLOPE", "ASPECT_NORTH", "ASPECT_EAST")
)
head(indic)## CL_UNCL PLOT_NUM P_LOCATION T_LOCATION GRADE_CAT TOT_HEIGHT SLOPE
## 1 CLIMBED 1 FACE TR EASY 32 130
## 2 CLIMBED 1 FACE TR EASY 32 130
## 3 CLIMBED 1 FACE TR EASY 32 130
## 4 CLIMBED 1 FACE TR EASY 32 130
## 5 CLIMBED 1 FACE TR EASY 32 130
## 6 CLIMBED 1 FACE TR EASY 32 130
## ASPECT_NORTH ASPECT_EAST AllRichness AllShannon AllSimpson AllCover LRichness
## 1 -0.91 0.42 3 0.8628578 0.5484765 211.1111 3
## 2 -0.91 0.42 3 0.8628578 0.5484765 211.1111 2
## 3 -0.91 0.42 3 0.8628578 0.5484765 211.1111 2
## 4 -0.91 0.42 3 0.8628578 0.5484765 211.1111 2
## 5 -0.91 0.42 2 0.6730117 0.4800000 166.6667 3
## 6 -0.91 0.42 2 0.6730117 0.4800000 166.6667 2
## LShannon LSimpson LCover MRichness MShannon MSimpson MCover PRichness
## 1 0.8628578 0.5484765 211.11111 1 0 0 22.22222 NA
## 2 0.6730117 0.4800000 166.66667 1 0 0 22.22222 NA
## 3 0.6931472 0.5000000 66.66667 1 0 0 22.22222 NA
## 4 0.3767702 0.2187500 88.88889 1 0 0 22.22222 NA
## 5 0.8628578 0.5484765 211.11111 1 0 0 22.22222 NA
## 6 0.6730117 0.4800000 166.66667 1 0 0 22.22222 NA
## PShannon PSimpson PCover
## 1 NA NA NA
## 2 NA NA NA
## 3 NA NA NA
## 4 NA NA NA
## 5 NA NA NA
## 6 NA NA NA
The NA values mean that there were none of that taxa group present for that plot.
yay! You did it!
You can now preform analysis on your plot species richness, diversity and cover!
bonus tip you may want to use dplyr to further subset your data.
i.e. if I only wanted faceplots I could use:
face_indic <- indic %>%
group_by(P_LOCATION) %>%
filter(P_LOCATION %in% c("FACE")| n() == 1)the %in% means include.