R/lipidome_comparison_read_TF.R

Defines functions flip_df pretty_transpose

Documented in flip_df pretty_transpose

#' Transpose data frame with correct row- and column names.
#'
#' @description `pretty_transpose` transposes a data frame to a new data frame
#' @details
#' This function takes a data frame, transposes it and sets the former first column as the column names. Row names are automatically generated by the data.table::transpose() function.
#' @param input_df a data frame with character strings and numerics
#' @export
#' @examples
#' df <- as.data.frame(cbind(c("ID", "a", "b", "c", "d"), c("z", 1, 2, 3, 4), c("x", 5, 6, 7, 8)))
#' pretty_transpose(df)
pretty_transpose <- function(input_df){
  t_input_df <- data.table::transpose(data.table::as.data.table(input_df), make.names = 1)
  t_input_df <- as.data.frame(t_input_df)
  row.names(t_input_df) <- colnames(input_df[-1])
  t_input_df
}

#' Flip data frame
#'
#' @description `flip_df` takes a data frame with the columns c(Compound, Type, Filename, Status, Group, Area) and transposes it, so that each area value is assigned to one compound (columnwise) and one SID (row-wise). Additionally the group variable is saved to each sample.  Suitable for preprocessed csvs generated by Thermo Fisher Tracefinder.
#' @param df data frame. Contains the columns c(Compound, Type, Filename, Status, Group, Area).
#' @export
#' @examples
#' flip_df(tfData)
flip_df <- function(df){
  sample <- levels(df$Filename)
  new_df <- data.frame()
  for(i in 1:length(sample)){
    buffer <- subset(df, Filename == sample[i])
    buffer_df <- subset(buffer, select = c(Compound, Area))
    t_buffer_df <- pretty_transpose(buffer_df)
    t_buffer_df <- tibble::add_column(t_buffer_df, SID = sample[i], .before = 1)
    new_df <- rbind(new_df, t_buffer_df)
    rownames(new_df)[i] <- i
  }
  new_df
}
lisaschneider0509/lipidomeComparisonR documentation built on Aug. 12, 2020, 12:52 a.m.