交叉拼接列的简便方法

时间:2015-05-16 01:39:17

标签: r data-manipulation

假设我有两个data.frames

DataFrame 1:

 Subject    metric_1a   metric_1b   metric_1c
    a        933.9       884.3      474.5
    b        993.3       557.4      484.2
    c        822.4       575.5      848.2

DataFrame 2:

 Subject    metric_2a   metric_2b   metric_2c
    a        933.9       884.3        474.5
    b        993.3       557.4        484.2
    c        822.4       575.5        848.2

我想制作:

  Subject    metric_1a   metric_2a   metric_1b  metric_2b  metrics_1c  metric_2c 
     a         933.9       933.9       884.3       884.3      474.5       474.5
     b         993.3       993.3       557.4       557.4      484.2       484.2
     c         822.4       822.4       575.5       575.5      848.2       848.2

我怎样才能轻松做到这一点?

1 个答案:

答案 0 :(得分:1)

你也可以以类似的方式使用cbind

df1 <- data.frame(subject = c("a", "b", "c"), 
                  metric_1a = c(123, 456, 789), 
                  metric_1b = c(123, 456, 789), 
                  metric_1c = c(123, 456, 789))

df2 <- data.frame(subject = c("a", "b", "c"),
                  metric_2a = c(123, 456, 789),
                  metric_2b = c(123, 456, 789),
                  metric_2c = c(123, 456, 789))

cbind(df1[1], df1[2], df2[2], df1[3], df2[3], df1[4], df2[4])