在两个变量中将宽转换为长

在两个变量中将宽转换为长

我有一个这样的数据框

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))

我想得到
  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

我会做
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])

但我认为有一个更简单的方法

最佳答案

这是相对的,你认为更容易,但一个选项是使用 dplyrtidyr :

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

关于r - 在两个变量中将宽转换为长,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57632058/

10-15 18:04