Mercurial > repos > prog > lcmsmatching
diff dfhlp.R @ 6:f86fec07f392 draft default tip
planemo upload commit c397cd8a93953798d733fd62653f7098caac30ce
author | prog |
---|---|
date | Fri, 22 Feb 2019 16:04:22 -0500 |
parents | fb9c0409d85c |
children |
line wrap: on
line diff
--- a/dfhlp.R Wed Apr 19 10:00:05 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -if ( ! exists('remove.na.rows')) { # Do not load again if already loaded - - source('strhlp.R') - - ################# - # RENAME COLUMN # - ################# - - rename.col <- function(df, cur, new) { - - for (k in seq(cur)) { - i <- which(cur[k] == colnames(df)) - if (length(i) == 1) - colnames(df)[i] <- new[k] - } - - return(df) - } - - ################## - # REMOVE NA ROWS # - ################## - - remove.na.rows <- function(df) { - na.rows <- apply(is.na(df), MARGIN = 1, all) - return(df[ ! na.rows, , drop = FALSE]) - } - - ###################### - # MOVE COLUMNS FIRST # - ###################### - - df.move.col.first <- function(df, cols) { - not.cols <- setdiff(names(df), cols) - df[c(cols, not.cols)] - } - - ##################### - # MOVE COLUMNS LAST # - ##################### - - df.move.col.last <- function(df, cols) { - not.cols <- setdiff(names(df), cols) - df[c(not.cols, cols)] - } - - ################# - # READ CSV FILE # - ################# - - # Read CSV file and return a data.frame. - # file The path to the CSV file. - # header If TRUE, use first line as header line. - # check.names If TRUE, correct header (column) names in the data frame, by replacing non-ASCII characters by dot. - # stringsAsFactors If TRUE, replace string values by factors. - # trim.header If TRUE, remove whitespaces at beginning and of header titles. - # trim.values If TRUE, remove whitespaces at beginning and of string values. - # remove.na.rows If TRUE, remove all lines that contain only NA values. - df.read.csv <- function(file, header = TRUE, remove.na.rows = TRUE, check.names = TRUE, stringsAsFactors = TRUE, trim.header = FALSE, trim.values = FALSE) { - - # Call built-in read.csv() - df <- read.csv(file, header = header, check.names = check.names, stringsAsFactors = stringsAsFactors) - - # Clean data frame - df <- df.clean(df, trim.colnames = trim.header, trim.values = trim.values, remove.na.rows = remove.na.rows) - - return(df) - } - - ################## - # WRITE TSV FILE # - ################## - - df.write.tsv <- function(df, file, row.names = FALSE, col.names = TRUE) { - write.table(df, file = file, row.names = row.names, col.names = col.names, sep = "\t") - } - - #################### - # CLEAN DATA FRAME # - #################### - - df.clean <- function(df, trim.colnames = FALSE, trim.values = FALSE, remove.na.rows = FALSE) { - - # Remove NA lines - if (remove.na.rows) - df <- remove.na.rows(df) - - # Trim header - if (trim.colnames) - colnames(df) <- trim(colnames(df)) - - # Trim values - if (trim.values) - for (c in 1:ncol(df)) - if (typeof(df[[c]]) == 'character') - df[[c]] <- trim(df[[c]]) - - return(df) - } - -} # end of load safe guard