R/nanotidy.R

Defines functions nanotidy

Documented in nanotidy

#' A Tidy Function
#'
#' This function allows you tidy Nanosight data.  It will make the data go from
#' 'wide' to 'long'.
#' @param df A dataframe.
#' @param sep_var Numerical range of columns to tidy, specify "2:n" where n is the last column
#' @return Gathers values from 'wide' to 'long' format. Returns a dataframe with a
#' Sample and Count column.
#' @keywords aggregate, clean, tidy
#' @import dplyr
#' @export

nanotidy <- function(df, sep_var){

  col <- 2: ncol(df)

  tidy_df <- df %>%
    tidyr::gather(Sample, Count, col) %>%
    tidyr::separate(Sample, into = sep_var, sep = "_", convert = TRUE) %>%
    dplyr::mutate_at(vars(Count),as.numeric)

  if("Dilution" %in% colnames(tidy_df)){

    fac_var <- sep_var[!sep_var %in% "Dilution"]

    tidy_df %>%
      dplyr::mutate_at(vars(fac_var),as.factor) %>%
      dplyr::mutate(Dilution =  as.numeric(Dilution),
             True_count = Count * Dilution)

  } else{stop("Error: No \"Dilution\" column present.")
  }
}
nguyens7/tidyNano documentation built on Feb. 27, 2021, 4:14 p.m.