2

I know my question is simple but I've trying all morning and I can't get my head around it.

I have this data frame:

  GeneID Gene.Symbol01    Ratio.2h   Ratio.6h  Ratio.10h  Ratio.24h  Pvalue_2h
 1    174           FUT -0.23618761 -0.3276162 -0.1366940 -4.4899131 0.49045105
  Pvalue_6h Pvalue_10h   Pvalue_24h
 1 0.06128851 0.59995612 0.0001798584

And I need to pivot_longer all columns except GeneID and GeneSymbol. The resulting data frame should have 3 new columns. One with time: 2h, 6h, 10h, and 24h. Then two more columns with the ratio values and another one with the pvalues.

I know this should be done with a combinametion of names_to and names_pattern. I have tried many things but I can't get it.

Last thing I have tried was this:

pivot_longer(cols = -c(GeneID, Gene.Symbol01),
             names_to = c("Time", ".value"),
             names_pattern = "_")

Dput:

structure(list(GeneID = 174, Gene.Symbol01 = "FUT", Ratio.2h = -0.23618761, 
    Ratio.6h = -0.3276162, Ratio.10h = -0.136694, Ratio.24h = -4.4899131, 
    Pvalue_2h = 0.49045105, Pvalue_6h = 0.06128851, Pvalue_10h = 0.59995612, 
    Pvalue_24h = 0.0001798584), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"))
Matt
  • 7,255
  • 2
  • 12
  • 34
netlak
  • 97
  • 5

2 Answers2

2

Using tidyverse you might want to pivot_longer all variable from Ratio.2h to Pvalue_24h, then separate it into 2 columns.

library(tidyverse)

DF %>% 
  pivot_longer(Ratio.2h:Pvalue_24h, names_to = "var") %>%
  separate(var, into = c("type", "time"), sep = "_|\\.")

# # A tibble: 8 x 5
#   GeneID Gene.Symbol01 type   time      value
#    <dbl> <chr>         <chr>  <chr>     <dbl>
# 1    174 FUT           Ratio  2h    -0.236   
# 2    174 FUT           Ratio  6h    -0.328   
# 3    174 FUT           Ratio  10h   -0.137   
# 4    174 FUT           Ratio  24h   -4.49    
# 5    174 FUT           Pvalue 2h     0.490   
# 6    174 FUT           Pvalue 6h     0.0613  
# 7    174 FUT           Pvalue 10h    0.600   
# 8    174 FUT           Pvalue 24h    0.000180

then, change back it to wider format using pivot_wider

DF %>% 
  pivot_longer(Ratio.2h:Pvalue_24h, names_to = "var") %>%
  separate(var, into = c("type", "time"), sep = "_|\\.") %>%
  pivot_wider(names_from = "type", values_from = "value")

# # A tibble: 4 x 5
#   GeneID Gene.Symbol01 time   Ratio   Pvalue
#    <dbl> <chr>         <chr>  <dbl>    <dbl>
# 1    174 FUT           2h    -0.236 0.490   
# 2    174 FUT           6h    -0.328 0.0613  
# 3    174 FUT           10h   -0.137 0.600   
# 4    174 FUT           24h   -4.49  0.000180

Data

DF <- tribble(~GeneID, ~Gene.Symbol01,  ~Ratio.2h,   ~Ratio.6h, ~Ratio.10h, ~Ratio.24h, ~Pvalue_2h,~Pvalue_6h, ~Pvalue_10h,   ~Pvalue_24h,
              174, "FUT", -0.23618761, -0.3276162, -0.1366940, -4.4899131, 0.49045105,0.06128851, 0.59995612, 0.0001798584)
nurandi
  • 1,588
  • 1
  • 11
  • 20
0

using melt:

library(data.table)


melt(df,id.vars=1:2) %>% separate(variable,c("type","time"))) %>%
 pivot_wider(names_from =type,values_from= value)

Result:


#
# GeneID Gene.Symbol01 time   Ratio   Pvalue
#   <dbl> <fct>         <chr>  <dbl>    <dbl>
#1    174 FUT           2h    -0.236 0.490   
#2    174 FUT           6h    -0.328 0.0613  
#3    174 FUT           10h   -0.137 0.600   
#4    174 FUT           24h   -4.49  0.000180

user12256545
  • 2,755
  • 4
  • 14
  • 28