Search code examples
rreshape2

Convert wide to long in two variables


I have a dataframe like this

df= data.frame(a1 = c(1,2,3), a2 = c(4,5,6), b1 = c(1,2,3), b2= c(4,NaN,6), id = c(1,2,3))

I want to get

  id  a measure1 b  measure2
1  1 a1        1 b1        1
2  2 a1        2 b1        2
3  3 a1        3 b1        3
4  1 a2        4 b2        4
5  2 a2        5 b2      NaN
6  3 a2        6 b2        6

I can make

df1 = df[, c(1,2,5)]
df2 = df[, c(3,4,5)]
library(reshape2)
df1_long = melt(df1,id.vars= 'id', measure.vars=c("a1", "a2"),
                               variable.name="a",
                value.name="measure1")
df2_long = melt(df2,id.vars= 'id', measure.vars=c("b1", "b2"),
                variable.name="b",
                value.name="measure2")

df_new = cbind(df1_long, df2_long[, -1])

But I think there is an easier way


Solution

  • It's relative what you consider to be easier, but an option is to use dplyr and tidyr:

    df %>%
     select(id, starts_with("a")) %>%
     gather(a, measurement1, -id) %>%
     bind_cols(df %>%
                select(starts_with("b")) %>%
                gather(b, measurement2))
    
      id  a measurement1  b measurement2
    1  1 a1            1 b1            1
    2  2 a1            2 b1            2
    3  3 a1            3 b1            3
    4  1 a2            4 b2            4
    5  2 a2            5 b2          NaN
    6  3 a2            6 b2            6