0

This is a small sample of my dataset:

 Genes    Cell_Line      Value 
   myc           A1        233
   myc           A2        213
   myc           A3        541
   erk           A1        123
   erk           A2        245
   erk           A3        123

I would like to take the first column, then make the unique gene names as columns for the values

Example :

Cell_Line  myc  erk
       A1  233  123
       A2  213  245
       A3  541  123

I am trying to navigate around the dplyr package and the group function. I am not sure if that works.

zephryl
  • 14,633
  • 3
  • 11
  • 30
  • It seems that you have one unique vector here. Do you have some columns in your orgininal dataset or just one vector ? – Dimitri Apr 05 '23 at 11:47
  • Try `tidyr::pivot_wider(data, names_from = Genes, values_from = Value)`. See this thread for more about restructuring data in this way: [How to reshape data from long to wide format](https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format). – zephryl Apr 05 '23 at 12:13

1 Answers1

0
# Load the reshape2 package
library(reshape2)

# Load the dataset into a data frame
data <- data.frame(
  Genes = c("myc", "myc", "myc", "erk", "erk", "erk"),
  Cell_Line = c("A1", "A2", "A3", "A1", "A2", "A3"),
  Value = c(233, 213, 541, 123, 245, 123)
)

# Use the dcast() function to pivot the data frame and reshape it
new_data <- dcast(data, Cell_Line ~ Genes, value.var = "Value")

# Print the new dataset
print(new_data)

results

      Cell_Line erk myc
1        A1 123 233
2        A2 245 213
3        A3 123 541

tidyverse way:

# Use tidyr function to pivot the data frame
library(tidyr)

new_data <- data %>%
  pivot_wider(names_from = Genes, values_from = Value)

  Cell_Line   myc   erk
  <chr>     <dbl> <dbl>
1 A1          233   123
2 A2          213   245
3 A3          541   123
zephryl
  • 14,633
  • 3
  • 11
  • 30
szmple
  • 453
  • 1
  • 8