-
Notifications
You must be signed in to change notification settings - Fork 0
/
eco_species.R
40 lines (28 loc) · 1.8 KB
/
eco_species.R
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
## --------------------------------------------------------------
## Name: eco_species.R
## Description: Code to combine species composition data for each ecoregion,
## which is listed in separate .csv files (produced from Python
## code: )
## Author: R.S.C. Cooke, [email protected]
## Date: April 2016 -
## Outputs:
## --------------------------------------------------------------
# Set up required packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(plyr)
# plyr: used to add filenames (ecoregion codes) to list of csvs # calls: ldply
#### Mammals ######
csv_files <- dir(path = "~/ArcGIS/PhD/Ecoregion_CSVs", pattern = "csv$", full.names = TRUE)
empty_files <- csv_files[which(file.info(csv_files)$size<0.1)] # identify empty csvs
# function to add filename (ecoregion code) to new column named eco_code for each csv
read_csv_file <- function(filename){
ret <- read.csv(filename, header = FALSE)
ret$eco_code <- filename
ret}
eco1 <- ldply(csv_files, read_csv_file) # runs function read_csv_file for each csv in list
eco1$eco_code <- basename(eco1$eco_code) # remove path name from ecoregion csv files
eco1$eco_code <- gsub("_M.csv", "", eco1$eco_code) # remove _M.csv from end of ecoregion csv files path
eco1 <- eco1[-c(1:2,31:32)] # drop geographic coordinates and empty columns
# Rename columns with shorter, lower case names - match column names in ArcGIS attribute tables
names(eco1) <- c("OBJECTid", "id_no", "binomial", "presence", "origin", "seasonal", "compiler", "year", "citation", "source", "dist_comm", "island", "subspecies", "subpop", "legend", "tax_comm", "kingdom_na", "phylum_nam", "class_name", "order_name", "family_nam", "genus_name", "species_na", "code", "shape_Leng", "Shape_Length", "Shape_Area", "eco_code")
#saveRDS(eco1, "eco.rds")