R/sort_components.R

sort_components = function (components) {

  # ---- Transpose the matrix to sort all components by columns ----
  components_long = stats::reshape(components %>%
                                     dplyr::mutate(threshold = format(threshold, digits = 16, trim = TRUE)) %>%
                                     dplyr::select(threshold, id, components),
                                   timevar = "threshold",
                                   idvar = "id",
                                   direction = "wide")

  # ---- Sort columns from the last to the first ----
  cols_components_long = names(components_long)[ncol(components_long):2]
  components_long_sorted = components_long[ do.call(order,
                                                    c(components_long[,cols_components_long], decreasing = TRUE) ),
                                            ] %>% dplyr::mutate(nr = row_number() )

  # ---- Create the variable re components ----
  for( column in cols_components_long){
    components_long_sorted = components_long_sorted %>%
      dplyr::group_by_(column) %>%
      dplyr::mutate(center_components = row_number() + min(nr) - 1) %>%
      dplyr::mutate_( .dots=setNames(  paste0("mean(center_components)"),
                                paste0("re", column )  ) ) %>%
      dplyr::ungroup() %>%
      dplyr::select_(paste0("-", column), "-center_components" )
  }

  # ---- Rename and create the variable plot_components used in the plot ----
  components_new_var = reshape2::melt( components_long_sorted %>% dplyr::select(-nr),  id=c("id")) %>%
    dplyr::rename(plot_components = value) %>%
    dplyr::mutate(threshold = as.numeric(gsub("recomponents.","", variable)),
           plot_components = as.numeric(plot_components)) %>%
    dplyr::arrange(id, threshold) %>%
    dplyr::select(-variable)

  # ---- Merge with the original dataset ---
  return(merge(components, components_new_var))
}
danielalcaide/mclean documentation built on May 28, 2019, 7:51 p.m.