如何转置数据帧的一部分?

时间:2019-05-27 09:42:25

标签: r

假设我有一个如下数据框:

ID  DOMAIN.x  VISIT.x  DAY     DOMAIN.y   VISIT.y
1     A        C1D1     30        B         C2D1 

现在,我想获取一行.y变量的另一个数据。

ID  DOMAIN   VISIT    DAY
1     A       C1D1    30
2     B       C2D1    30

1 个答案:

答案 0 :(得分:0)

您可以这样做:

df <- read.table(text = "
ID  DOMAIN.x  VISIT.x  DAY     DOMAIN.y   VISIT.y
1     A        C1D1     30        B         C2D1 ", header = T)

reshape(df, direction = "long", varying = list(domain = c(2,5), visit = c(3,6)),
        idvar = "ID", v.names = c("DOMAIN", "VISIT"), timevar = "ID", times = c(1,2))

    ID DAY DOMAIN VISIT
1.1  1  30      A  C1D1
1.2  2  30      B  C2D1