1

I have a dataframe in R of data look likes this: source

and I want to transform the table to this target

What is the fastest way to do it in R?

Here is how I did it, but I admit that it's very long

method_vec <- c(rep("PCA", 6), rep("Specter", 6),rep("Seurat", 6),rep("Geometric sketching", 6),rep("dropClust", 6),rep("RtsneKmeans", 6),rep("TSCAN", 6))

memory_vec <- c(memory_data$Specter, memory_data$Seurat, memory_data$Geometric.sketching, memory_data$dropClust, memory_data$TSCAN, memory_data$PCA, memory_data$RtsneKmeans)

df <- data.frame("Cells" = rep(memory_data$N, 7), "Method" = method_vec, "Memory" = memory_vec)

Thanks!

1 Answers1

1

We can use pivot_longer

library(tidyr)
pivot_longer(df, cols = -N, names_to = 'Method', values_to = 'Memory')
akrun
  • 674,427
  • 24
  • 381
  • 486
  • This is the best answer ever. Thank you so much. Do you know any source to learn such a thing? – Thomas Edison Oct 23 '20 at 20:04
  • 1
    @ThomasEdison thank you. You can check the [vignettes](https://tidyr.tidyverse.org/articles/pivot.html) of tidyverse where all the features and its examples are given – akrun Oct 23 '20 at 20:06