diff --git a/NAMESPACE b/NAMESPACE index 61de488..74752ad 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,6 +10,7 @@ export(create_datastore_script) export(fix_utc_offset) export(fuzz_location) export(generate_ll_from_utm) +export(get_columns_from_files) export(get_custom_flags) export(get_dc_flags) export(get_df_flags) diff --git a/NEWS.md b/NEWS.md index 3a11070..aabf5e7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# QCkit v0.1.8 +2024-05-29 +* New funcion `get_columns_from_files` added. Function produces either 1) a dataframe of all column names within your files or 2) a list of common columns across your files. + # QCkit v0.1.7 2024-04-17 * Major updates to the DRR template including: using snake case instead of camel case for variables; updating Table 3 to only display filenames only when there are multiple files, fixed multiple issues with footnotes, added citations to NPSdataverse packages, added a section that prints the R code needed to download the data package and load it in to R. diff --git a/R/get_columns_from_files.R b/R/get_columns_from_files.R new file mode 100644 index 0000000..32eb9ef --- /dev/null +++ b/R/get_columns_from_files.R @@ -0,0 +1,79 @@ +#' Retrieve all columns from each dataset and columns that occur more than once across those datasets +#' +#' @description `get_columns_from_files()` produces two dataframes: one that lists all columns from within each of the .csv's in a specified working directory and another that lists all columns which appear more than once across those .csv's. +#' +#' @details `get_columns_from_files()` can be used as an initial step in data processing, particularly if the goal of the data processing is to merge multiple files into one, larger flat file. The function allows the user to preview and list out what columns exist within any given number of .csv's in a format that is more digestible. If the user chooses, they can also find commonalities across the columns in those files, highlighting any columns that serve as key variables upon which dataframes can then be joined. +#' +#' @param wd String. Your specified working directory; points to the directory where the .csv files you want to work with live. I.e., `getwd()`, `setwd("./data examples")`. +#' +#' @param common Logical. Defaults to `FALSE`. In default status, the function returns a full dataframe of all columns within each of the files in your working directory. If set to `TRUE`, the function returns a single list of columns that occur more than once across all of your files. +#' +#' @return one of two dataframes. If common set to `FALSE`, returns a dataframe of all columns in the files within your working directory. If common set to `TRUE`, returns a list of common columns across your files. +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' get_columns_from_files(wd = setwd("./data examples"), common = TRUE) +#' } +#' +get_columns_from_files <- function(wd = getwd(), common = FALSE) { + + fileList <- list.files(path = wd, pattern = "\\.csv$") # create a list with the names of all csv's + + dfList <- suppressMessages(lapply(fileList, readr::read_csv)) # create a nested list of all datasets + + names(dfList) <- fileList # name each list within dfList by the name of it's respective file name + + maxCols <- list() # create an empty list to find the max number of columns from the provided datasets + + for(i in 1:max(length(dfList))){ + maxCols[i] <- length(names(dfList[[i]])) + } # fill in the list with the number of columns from each dataset + + maxCols <- max(unlist(maxCols)) # find the max column number from the list + + allCols <- tibble::tibble(.rows = maxCols) # create an empty dataframe with the maximum number of rows being the maximum number of columns from the datasets provided + + for(i in 1:max(length(dfList))){ + + columns <- names(dfList[[i]]) + + length(columns) <- maxCols + + tempdf <- tibble::tibble(columns) + + names(tempdf)[1] <- names(dfList)[i] + + allCols <- cbind(allCols, tempdf) + + } # add a list of all columns from each provided dataset to the empty dataframe + + df <- unlist(allCols) + + y <- list() + z <- list() + + for(i in 1:length(df)){ + + y[i] <- length(which(df == df[i])) + + } # find how many times a value within the column names is repeated. Generates a list with the number of times each respective column name is repeated across all column names + + + for(i in 1:length(y)){ + ifelse(y[i] > 1, + z[i] <- df[i], + next) + } # if a column name appears more than once across all column names, capture that column in a list (z) + + z <- Filter(Negate(is.null), z) # get rid of Null values + commonCols <- z %>% unique() %>% tibble::tibble() # Filter to a unique list of the column names that appear more than once + + colnames(commonCols)[1] <- "Common Columns" + + ifelse(common == FALSE, + return(allCols), + return(commonCols) + ) +} diff --git a/docs/404.html b/docs/404.html index bbe9a71..4210397 100644 --- a/docs/404.html +++ b/docs/404.html @@ -6,7 +6,7 @@
Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
diff --git a/docs/articles/DRR_Purpose_and_Scope.html b/docs/articles/DRR_Purpose_and_Scope.html index b45398f..c7246fc 100644 --- a/docs/articles/DRR_Purpose_and_Scope.html +++ b/docs/articles/DRR_Purpose_and_Scope.html @@ -6,7 +6,7 @@
+
Workflow for data collection, processing, dissemination, and use for general studies. Teal-colored boxes are subject to reproducibility requirements. @@ -375,7 +375,7 @@

+
Workflow for data collection, processing, dissemination, and use for vital sign monitoring efforts. Teal-colored boxes are subject to reproducibility requirements. @@ -396,7 +396,7 @@

+
Workflow for data collection, processing, dissemination, and use for inventory studies. Teal-colored boxes are subject to reproducibility requirements. @@ -431,7 +431,7 @@
Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
+

After selecting “OK” two things will happen: First, you the DRR Template file will open up. It is called “Untitled.Rmd” by default. @@ -223,7 +223,7 @@
Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.

+
Adding Citations - Source vs. Visual editing of the Template and how to access the citation manager.

+
Adding Citations - Using the citation manager.
Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
diff --git a/docs/articles/index.html b/docs/articles/index.html index dc24a3b..ea5753e 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -1,5 +1,5 @@ -NEWS.md
+ 2024-05-29 * New funcion get_columns_from_files added. Function produces either 1) a dataframe of all column names within your files or 2) a list of common columns across your files.
2024-04-17 * Major updates to the DRR template including: using snake case instead of camel case for variables; updating Table 3 to only display filenames only when there are multiple files, fixed multiple issues with footnotes, added citations to NPSdataverse packages, added a section that prints the R code needed to download the data package and load it in to R. * Updated the DRR documentation to account for new variable names.
@@ -158,7 +162,7 @@Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
diff --git a/docs/reference/convert_utm_to_ll.html b/docs/reference/convert_utm_to_ll.html index b3c7c33..438023b 100644 --- a/docs/reference/convert_utm_to_ll.html +++ b/docs/reference/convert_utm_to_ll.html @@ -1,5 +1,5 @@ -Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
diff --git a/docs/reference/create_datastore_script.html b/docs/reference/create_datastore_script.html index 4eabec8..74e4ba3 100644 --- a/docs/reference/create_datastore_script.html +++ b/docs/reference/create_datastore_script.html @@ -1,5 +1,5 @@ -Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
diff --git a/docs/reference/get_columns_from_files.html b/docs/reference/get_columns_from_files.html new file mode 100644 index 0000000..08abb8b --- /dev/null +++ b/docs/reference/get_columns_from_files.html @@ -0,0 +1,127 @@ + +R/get_columns_from_files.R
+ get_columns_from_files.Rdget_columns_from_files() produces two dataframes: one that lists all columns from within each of the .csv's in a specified working directory and another that lists all columns which appear more than once across those .csv's.
get_columns_from_files(wd = getwd(), common = FALSE)String. Your specified working directory; points to the directory where the .csv files you want to work with live. I.e., getwd(), setwd("./data examples").
Logical. Defaults to FALSE. In default status, the function returns a full dataframe of all columns within each of the files in your working directory. If set to TRUE, the function returns a single list of columns that occur more than once across all of your files.
one of two dataframes. If common set to FALSE, returns a dataframe of all columns in the files within your working directory. If common set to TRUE, returns a list of common columns across your files.
get_columns_from_files() can be used as an initial step in data processing, particularly if the goal of the data processing is to merge multiple files into one, larger flat file. The function allows the user to preview and list out what columns exist within any given number of .csv's in a format that is more digestible. If the user chooses, they can also find commonalities across the columns in those files, highlighting any columns that serve as key variables upon which dataframes can then be joined.
if (FALSE) {
+get_columns_from_files(wd = setwd("./data examples"), common = TRUE)
+}
+
+R/summarize_qc_flags.R
get_dc_flags.RdSite built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
diff --git a/docs/reference/get_dp_flags.html b/docs/reference/get_dp_flags.html index e797cdb..2ae4c95 100644 --- a/docs/reference/get_dp_flags.html +++ b/docs/reference/get_dp_flags.html @@ -1,5 +1,5 @@ -Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
diff --git a/docs/reference/get_elevation.html b/docs/reference/get_elevation.html index 8bf2195..bb5a78e 100644 --- a/docs/reference/get_elevation.html +++ b/docs/reference/get_elevation.html @@ -1,5 +1,5 @@ -generate_ll_from_utm()
Coordinate Conversion from UTM to Latitude and Longitude
Retrieve all columns from each dataset and columns that occur more than once across those datasets
Site built with pkgdown 2.0.6.
+Site built with pkgdown 2.0.9.
diff --git a/docs/reference/order_cols.html b/docs/reference/order_cols.html index a649648..eb51218 100644 --- a/docs/reference/order_cols.html +++ b/docs/reference/order_cols.html @@ -1,5 +1,5 @@ -