tidyverse
I use to hate R till I learned some python and started playing with TidyVerse which just seems to make sense and I even get the grammar idea.
Split a column into two columns
Split a column into two columns
# This uses tidyr::separate
# remove=FALSE ensures the original column is maintained in the new object, if you want to remove it use remove=TRUE
data %>% separate(ColumnName, c("NewColumn1", "NewColumn2"), sep = "SplitCharacter", remove=FALSE)
Using TidyVerse Tools in Functions
Using TidyVerse Tools in Functions
It turns out that using different functions in tidyverse in functions is much more complicated than I think it should be but I guess that is life. One of the best documents walking through the issue is from Hadley Wikham as you might expect (https://rpubs.com/hadley/dplyr-programming), and is the best solution I've found on the web except it didn't seem to work for dplyr::filter in my hands and I found a different solution
# Function that will work with dplyr::filter dynamically in an R function
summarize_table <- function(df, data_type, denom_string, numerator_string, detected_value, assay, event) {
### What does UQ() do to make this work?
## Get the dynamic Total Count number by the specific assay
denom_name <- rlang::sym(denom_string)
total_count <- df %>%
filter(UQ(denom_name) == UQ(detected_value)) %>%
count()
total_count <- as.integer(total_count)
## Get the dynamic Positive Count number
column_name <- rlang::sym(numerator_string)
observed_count <- df %>%
filter(UQ(column_name) == UQ(detected_value)) %>%
count()
observed_count <- as.integer(observed_count)
## Assign results to existing object
# Use <<- to assign the added rows to an existing object
common_genetics <<- common_genetics %>%
add_row(Type = data_type,
Event = event,
Source = assay,
Total_Count = total_count,
Positive_Count = observed_count)
}