From 1c416665302a8484b6c6f95e7188d87aabf7abd4 Mon Sep 17 00:00:00 2001 From: Alejandro Ruete Date: Fri, 15 Jan 2021 15:08:24 +0100 Subject: [PATCH] fix uniqueSpillover and example orgDate --- R/organizeBirds.R | 5 ++++- R/overlayBirds.R | 5 +++++ R/summarizeBirds.R | 36 ++++++++++++++++++------------------ man/organizeDate.Rd | 5 ++++- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/R/organizeBirds.R b/R/organizeBirds.R index b1e889a..9510c3b 100644 --- a/R/organizeBirds.R +++ b/R/organizeBirds.R @@ -45,7 +45,10 @@ findCols <- function(pattern, df, exact=FALSE, value = TRUE){ #' @export #' @examples #' ymd<-as.Date(Sys.Date())+1:5 -#' organizeDate(as.data.frame(ymd), "ymd") +#' id<-1:5 +#' organizeDate(data.frame("id"=id, +#' "ymd"=as.character(ymd)), +#' "ymd") #' @keywords internal organizeDate <- function(x, columns){ if (!length(columns) %in% c(1,3)) stop("Could not create date, please specify either one or three column names") diff --git a/R/overlayBirds.R b/R/overlayBirds.R index 5d874ca..04ed8af 100644 --- a/R/overlayBirds.R +++ b/R/overlayBirds.R @@ -76,6 +76,11 @@ includeUniqueSpillover <- function(birdData, grid, visitCol){ stop("Organized data and grid do not share the same CRS") } + ## rename grids id no have integers + for(i in 1:length(grid)){ + slot(slot(grid, "polygons")[[i]], "ID") <- as.character(i) + } + #Extract the unique ID from the polygons in the spdf ids <- data.frame( matrix( diff --git a/R/summarizeBirds.R b/R/summarizeBirds.R index 9f35fc5..45c6473 100644 --- a/R/summarizeBirds.R +++ b/R/summarizeBirds.R @@ -70,21 +70,24 @@ getSpatial<-function(birdOverlay, visitCol=NULL){ cols2use<-c("scientificName", "year", "month", "day", visitCol) - dataRes<-lapply(dataList[birdOverlay$nonEmptyGridCells], function(x){ - - x<-x[,cols2use] - colnames(x) <- c("scientificName", "year", "month", "day", "visitCol") - - return(c("nObs"=length(x[,"scientificName"]), - "nVis"=length(unique(x[,"visitCol"])), - "nSpp"=length(unique(x[,"scientificName"])), - "avgSll"= median(summarise(group_by(x, visitCol), - avgSLL=n_distinct(.data$scientificName))$avgSLL), - "nDays"=length(unique( paste0(x[,"year"],"-", as.numeric(x[,"month"]), "-", as.numeric(x[,"day"]))) ), - "nYears"=length(unique(x[,"year"])), - "visitsUID"= paste0(unique(x[,"visitCol"]), collapse = ",") - )) - }) + dataRes<-lapply(dataList[birdOverlay$nonEmptyGridCells], + function(x){ + x<-x[,cols2use] + colnames(x) <- c("scientificName", "year", "month", "day", "visitCol") + + tmp <- c("nObs"=length(x[,"scientificName"]), + "nVis"=length(unique(x[,"visitCol"])), + "nSpp"=length(unique(x[,"scientificName"])), + "avgSll"= median(summarise(group_by(x, .data$visitCol), + avgSLL=n_distinct(.data$scientificName))$avgSLL), + "nDays"=length(unique( paste0(x[,"year"],"-", + as.numeric(x[,"month"]), "-", + as.numeric(x[,"day"]))) ), + "nYears"=length(unique(x[,"year"])), + "visitsUID"= paste0(unique(x[,"visitCol"]), collapse = ",") + ) + return(tmp) + }) dataRes<-data.frame(matrix(unlist(dataRes), nrow=length(dataRes), byrow=TRUE), @@ -314,7 +317,6 @@ summarizeBirds.OrganizedBirds<-function(x, grid, spillOver = NULL){ stop("The variable grid can only be of class SpatialPolygonsDataFrame, or NULL") } }else{ - areaGrid <- OB2Polygon(x) bTOver <- overlayBirds(x, grid=areaGrid, spillOver = spillOver) warning("To get the most out of summarizeBirds you should have a grid.") @@ -322,7 +324,6 @@ summarizeBirds.OrganizedBirds<-function(x, grid, spillOver = NULL){ #Here we use a modifyed version of bOver where the grid is only one single cell. temporal <- getTemporal(bTOver) - if(useSpatial){ #If we have spatial grid data: spatial <- getSpatial(bOver) @@ -333,7 +334,6 @@ summarizeBirds.OrganizedBirds<-function(x, grid, spillOver = NULL){ spatioTemporal <- getSpatioTemporal(bTOver, visitCol=visitCol) wNonEmptyCells <- bTOver$nonEmptyGridCells } - res <- list("temporal" = temporal, "spatial" = spatial, "spatioTemporal" = spatioTemporal$resYM, diff --git a/man/organizeDate.Rd b/man/organizeDate.Rd index c7ac8c7..3e677f5 100644 --- a/man/organizeDate.Rd +++ b/man/organizeDate.Rd @@ -23,6 +23,9 @@ Organize the date-column(s) in a dataframe to three columns } \examples{ ymd<-as.Date(Sys.Date())+1:5 -organizeDate(as.data.frame(ymd), "ymd") +id<-1:5 +organizeDate(data.frame("id"=id, + "ymd"=as.character(ymd)), + "ymd") } \keyword{internal}