install logo correctly
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
# Functions for connecting to Civilytics' data sources
|
||||
|
||||
|
||||
|
||||
# postgresCon <- function(myip){
|
||||
# require("RPostgreSQL")
|
||||
# require("pool")
|
||||
# drv <- dbDriver("PostgreSQL")
|
||||
# con <- dbPool(drv, dbname = "crime", host = myip,
|
||||
# port = 5432, user="admin", password = "betterplacemakeworld")
|
||||
# return(con)
|
||||
# }
|
||||
|
||||
|
||||
#' Clean unreported data counts in UCR data
|
||||
#'
|
||||
#' @param x a column of data, if character, substitute and conver to numeric
|
||||
#'
|
||||
#' @return a numeric column of data
|
||||
#' @export
|
||||
countCleanr <- function(x){
|
||||
if(class(x) == "character"){
|
||||
x[x == "None not reported"] <- "0"
|
||||
x[x == "Not applicable"] <- NA
|
||||
x <- as.numeric(x)
|
||||
return(x)
|
||||
} else{
|
||||
return(x)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#' Find columns in a dataframe that have values with a "."
|
||||
#'
|
||||
#' @param data a dataframe
|
||||
#'
|
||||
#' @return the names of columns in the dataframe with one or more entries equal to "."
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
findDots <- function(data){
|
||||
return(names(data)[lapply(data, countDots) > 0])
|
||||
|
||||
}
|
||||
|
||||
#' Count the number of single period entries in a vector
|
||||
#'
|
||||
#' @param x a character vector
|
||||
#'
|
||||
#' @return
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
countDots <- function(x){
|
||||
len <- length(x[x == "." & !is.na(x)])
|
||||
totlen <- length(x)
|
||||
return(len)
|
||||
}
|
||||
|
||||
|
||||
#' Make names safe for inclusion in a database
|
||||
#'
|
||||
#' @param names a character vector of possible column or table names
|
||||
#'
|
||||
#' @return a vector the same length as the input vector with clean names
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
dbSafeNames <- function(names) {
|
||||
names = gsub('[^a-z0-9]+','_',tolower(names))
|
||||
names = make.names(names, unique = TRUE, allow_ = TRUE)
|
||||
names = gsub('.','_',names, fixed = TRUE)
|
||||
names
|
||||
}
|
||||
|
||||
#' Count number of missing values in a vector
|
||||
#'
|
||||
#' @param x a vector of values
|
||||
#'
|
||||
#' @return an integer equal to the number of NA entries in the vector
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' countNA(letters) # = 0
|
||||
#' countNA(c(letters, NA)) # = 1
|
||||
countNA <- function(x){
|
||||
length(x[is.na(x)])
|
||||
}
|
||||
|
||||
|
||||
#' Count the number of unique values in a vector
|
||||
#'
|
||||
#' @param x a vector of values
|
||||
#'
|
||||
#' @return an integer equal to the number of unique values in a vector
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' nvals(letters) # = 26
|
||||
#' nvals(state.abb) # = 50
|
||||
nvals <- function(x){
|
||||
length(unique(x))
|
||||
}
|
||||
|
||||
|
||||
#' Capitalize a character string after each space
|
||||
#'
|
||||
#' @param x a character string
|
||||
#'
|
||||
#' @return a vector of characters with all values after a space capitalized
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' my_string <- c("Happy school", "Easy school", "cool School", "big school")
|
||||
#' simpleCap(my_string)
|
||||
simpleCap <- function(x) {
|
||||
stopifnot(class(x) == "character")
|
||||
s <- strsplit(x, " ")[[1]]
|
||||
paste(toupper(substring(s, 1,1)), substring(s, 2),
|
||||
sep = "", collapse = " ")
|
||||
}
|
||||
Reference in New Issue
Block a user