#' creed
#'
#' This function reads in CSVs and allows you to specify certain columns to import.
#' It is faster than fread when selecting specific columns.
#'
#' @param data the path/name of csv you wish to read in (same format as you would use for read.csv)
#' @param select list of column names you wish you bring in (i.e. c("COLUMN1", "COLUMN3")). If you run without a select statement, creed will return a data frame with a data frame of all the column names so you can figure out what columns exist in the csv
#' @examples
#' #run without select statement to get column names:
#' raw.data <- system.file("extdata", "DATA.csv", package = "creed", mustWork = TRUE)
#' print(creed(raw.data))
#' #run with select statement to get specific columns:
#' raw.data <- system.file("extdata", "DATA.csv", package = "creed", mustWork = TRUE)
#' print(creed(raw.data, select = c("COLUMN1", "COLUMN3")))
#' #run with select = "ALL" to get all columns:
#' raw.data <- system.file("extdata", "DATA.csv", package = "creed", mustWork = TRUE)
#' print(creed(raw.data, select = "ALL"))
#' @export
creed <- function(data, select){
if (missing(select)) {
data1 <- read.csv(data, nrows = 1)
result <- data.frame("COLNAMES" = names(data1))
warning("Select statement missing; returning column names")
return(result)
} else if (any(select == "ALL")){
result <- read.csv(data)
}else {
head.only <- read.csv(data,nrows=1) #get first row of csv
cols.num <- which(names(head.only) %in% select) #get column numbers where column name is in select array
c.frame <- data.frame(seq(1, ncol(head.only), by = 1) ) #create data frame with one row for each column in data
c.frame$cols <- "NULL" #create another row with all values as NULL
c.frame$cols[cols.num] <- NA #change rows where column number is in select array to NA
string <- c.frame$cols #get new column as string. This should be a string the same length as the total number of columns in the
#original dataset with NULL for columns that are not wanted and NA for columns that are wanted, i.e. if you want columns 1,3 & 5
#the string will be c(NA, NULL, NA, NULL, NA)
result <- read.csv(data, colClasses = string) #pass string as "colClasses" argument for read.csv
return(result)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.