Search code examples
rreshape2melt

How to bind columns based on character string


I am trying to convert my data frame from wide to long form based on a column's character string. In the example below, I want O2_222.coefficients.x1 and O2_217.coefficients.x1 in a single column, O2_222.R-squared and O2_217.R-squared in a single column and RunTime as it's own. Attempted using melt in the reshape2 package but can't seem to get the syntax right.

structure(list(O2_222.coefficients.x1 = c(0.494206524044508, 0.351865091962266,
0.348933739038027, 0.412232161883577, 0.702783684327072), `O2_222.R-squared` = 
c(0.922054236839182, 0.915753625911676, 0.91109476704698, 0.917998834313392, 
0.967759465780247), O2_217.coefficients.x1 = c(0.390012278483346, 0.0694948285748309, 
0.0323121611694059, 0.0372526286990146, 0.194291648564898), `O2_217.R-squared` = 
c(0.921256057864199, 0.537913087580067, 0.271398305115866, 0.274339042666519, 
0.908338928665188), RunTime = c(9.03, 14.08, 19.08, 24.08, 29.08)), row.names = c(116L, 
216L, 316L, 416L, 516L), class = "data.frame")

Solution

  • We could use pivot_longer from tidyr and specify the names_sep as .

    library(tidyr)
    library(dplyr)
    df1 %>%
       pivot_longer(cols = -RunTime, names_to = c('group', '.value'), names_sep="\\.")
    # A tibble: 10 x 4
    #   RunTime group  coefficients `R-squared`
    #     <dbl> <chr>         <dbl>       <dbl>
    # 1    9.03 O2_222       0.494        0.922
    # 2    9.03 O2_217       0.390        0.921
    # 3   14.1  O2_222       0.352        0.916
    # 4   14.1  O2_217       0.0695       0.538
    # 5   19.1  O2_222       0.349        0.911
    # 6   19.1  O2_217       0.0323       0.271
    # 7   24.1  O2_222       0.412        0.918
    # 8   24.1  O2_217       0.0373       0.274
    # 9   29.1  O2_222       0.703        0.968
    #10   29.1  O2_217       0.194        0.908