-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Function Added To Get Columns From Files #106
Open
iquevedo123
wants to merge
9
commits into
nationalparkservice:master
Choose a base branch
from
iquevedo123:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f827232
Created .R file for new function and added initial documentation
iquevedo123 a492acd
Rewrite descriptions and add in function code
iquevedo123 906777a
Add in and format roxygen documentation
iquevedo123 e494853
Proper formatting of documentation as is recommended in the tidyverse…
iquevedo123 e73d142
added function to namespace and updated .rd
iquevedo123 2f5ce71
Updated Github site and articles
iquevedo123 034eded
News updated
iquevedo123 bf78ccb
Fixed spacing on documentation
iquevedo123 e6c2d4e
Updated documentation after running through lintr
iquevedo123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this should be called
get_column_names()
rather thanget_columns_from_files()
as I think it returns the column names, not the columns themselves (and update associated documentation to clarify this point)