'Read in multiple csvs from folder, convert them to spatialpolgyonsdataframes based on conditions
I am trying create a loop that reads in a number of csv files (8) and convert them to spatialpolygonsdataframes, intersects them with an animal home range and extracts some information of interest. However, I am trying to read the csv's in based on conditions. For example, I want to read in the csv if the data (year) contained in the csv matches the year(s) of telemetry data for an individual and then apply a few lines of code to this animal. If the climate years do not match with the individual, then the loop would continue on to the next animal and run through the years of climate data that match and so on..
I have done this for one animal in a step by step approach but I would like to expand it to 8 csvs and multiple animals (10) where the loop includes a condition that if the climate year matches the telemetry year then the code will be applied.
Below a step by step example for 1 animal:
ClimateVars <-fread("ClimatePoints_Year_2020m.csv",verbose=T)
#Create a spatial points file
#create coords as a dataframe which only includes lat and longs pulled from ClimateVars
coords <- as.data.frame(cbind(ClimateVars$Longitude, ClimateVars$Latitude))
#create spatialpoints data frame with Cordinates from coords, bring data over from climatevars and assign the projection
climatePoints <- SpatialPointsDataFrame(coords = coords, data = ClimateVars, proj4string =
CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
#transform Cliamtepoints Spatial Points Data frame to albers using sptransform
climatePoints_albers <-spTransform(climatePoints,CRS("+init=EPSG:3005"))
#Now transform the CRS for the moose home range to match that of the ClimatePpints Albers file
thisMooseUD2Polygont <-spTransform(thisMooseUD2Polygon,CRS("+init=EPSG:3005")) #project in same albers projection as points
#grab the climate variables that are associated with each year of HR
ClimateinHR <- raster::intersect(climatePoints_albers[climatePoints_albers$Year== mooseYr,], thisMooseUD2Polygont)
I have tried to create a loop with the following but I am stuck on how to get the csv year to match the telemetry year, run the code, and then move on to the next animal..
temp = list.files(pattern="*.csv")
for(i in 1:length(temp)) {
Climatedata <- read.csv(temp[i], header=TRUE, sep = ",", stringsAsFactors = FALSE)
Climate.year <- unique(Climatedata$Year)
#how to match year of df and yr of climate in the loop??
coords <- as.data.frame(cbind(Climatedata$Longitude, Climatedata$Latitude)) #select coordinates from the file
climatepoints <- SpatialPointsDataFrame(coords = coords, data = Climatedata, proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
#Now transform the CRS for the moose home range to match that of the ClimatePpints Albers file
TransmooseHR <- spTransform(mooseHR, CRS("+init=EPSG:3005"))
#grab the climate variables that are associated with each year of HR
ClimateinHR <- raster::intersect(climatePoints_albers[climatePoints_albers$Year== mooseYr,], TransmooseHR)
##.......
I am very new to dealing with spatial data in R, so apologies in advance. I am not sure if an attach(), detach() function would work for my case.. The csv contains a year column. I have also named the csvs as the year of data it contains. Example 'Year2020.csv', 'Year2019.csv' etc.
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
