-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ImmuneDynamics/development
Development
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
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
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) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.