Skip to content

Commit

Permalink
Merge pull request #42 from ImmuneDynamics/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
tomashhurst authored Jun 3, 2021
2 parents dcac605 + 074f0c7 commit 3094c45
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(do.aggregate)
export(do.align)
export(do.asinh)
export(do.clip)
export(do.combine.cols)
export(do.embed.columns)
export(do.filter)
export(do.list.summary)
Expand Down
53 changes: 53 additions & 0 deletions R/do.combine.cols.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#' do.combine.cols - Combine the values of two columns into a single column
#'
#' This function allows you to combine the values of two columns, with an option to remove any 'NA' values
#'
#' @usage do.combine.cols(dat, col1, col2, na.rm)
#'
#' @param dat NO DEFAULT. Data.table.
#' @param col1 NO DEFAULT. Name of the column you want to combine into.
#' @param col2 NO DEFAULT. Name of the column you want to combine from. This column will be deleted after combination into col1.
#' @param na.rm DEFAULT = TRUE. If NA values are present in the columns, they will be removed.
#'
#' @return Returns a data.table with adjusted columns
#'
#' @author Thomas M Ashhurst, \email{thomas.ashhurst@@sydney.edu.au}
#'
#' @export

do.combine.cols <- function(dat, col1, col2, na.rm = TRUE){

### NA checks

na.checks <- rowSums(is.na(dat[,c(col1, col2), with = FALSE]))

if(any(na.checks < 1)){
stop("Less that one 'NA' per row")
}

### Check class of col1

cls <- class(dat[[col1]])

### Column combination

if(isTRUE(na.rm)){
dat[[col1]][is.na(dat[[col1]])] <- 'PLACEHOLDER'
dat[[col2]][is.na(dat[[col2]])] <- 'PLACEHOLDER'
dat[[col1]] <- paste0(dat[[col1]], dat[[col2]])
dat[[col1]] <- gsub('PLACEHOLDER', '', dat[[col1]])
dat[[col2]] <- NULL
} else {
dat[[col1]] <- paste0(dat[[col1]], dat[[col2]])
dat[[col2]] <- NULL
}

### Class adjust

class(dat[[col1]]) <- cls

### Return

return(dat)

}
26 changes: 26 additions & 0 deletions man/do.combine.cols.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3094c45

Please sign in to comment.