转换R中数据帧的格式(排序)

时间:2014-11-26 21:13:55

标签: r dataframe reshape

我有一个以下格式的数据框:

StudentID     Name     School     Q1     Q2     Q3

1             abc      xyz        1      5      3 
2             abc2     xyz2       2      4      6
3             abc3     xyz3       1      2      5

我想将其转换为以下内容:

StudentID     Name     School     Q
1             abc      xyz        1
1             abc      xyz        5
1             abc      xyz        3
2             abc2     xyz2       2
2             abc2     xyz2       4
2             abc2     xyz2       6
3             abc3     xyz3       1
3             abc3     xyz3       2
3             abc3     xyz3       5

有人可以给我示例代码在R中使用它吗?

3 个答案:

答案 0 :(得分:3)

您可以使用melt

library(reshape2)    # for melt()
library(dplyr)       # for arrange()

arrange(melt(df, c("StudentID", "Name", "School"), value.name = "Q"), StudentID)
#   StudentID Name School variable Q
# 1         1  abc    xyz       Q1 1
# 2         1  abc    xyz       Q2 5
# 3         1  abc    xyz       Q3 3
# 4         2 abc2   xyz2       Q1 2
# 5         2 abc2   xyz2       Q2 4
# 6         2 abc2   xyz2       Q3 6
# 7         3 abc3   xyz3       Q1 1
# 8         3 abc3   xyz3       Q2 2
# 9         3 abc3   xyz3       Q3 5

如果您不需要/想要它,您可以取出variable列。

答案 1 :(得分:1)

base R解决方案:

  cbind(df[rep(1:nrow(df),each=3),1:3],Q=as.vector(t(df[,4:6])))  

答案 2 :(得分:1)

还有两个选项需要考虑:

选项1:来自基地R的reshape

reshape(mydf, direction = "long", varying = c("Q1", "Q2", "Q3"), sep = "")
#     StudentID Name School time Q id
# 1.1         1  abc    xyz    1 1  1
# 2.1         2 abc2   xyz2    1 2  2
# 3.1         3 abc3   xyz3    1 1  3
# 1.2         1  abc    xyz    2 5  1
# 2.2         2 abc2   xyz2    2 4  2
# 3.2         3 abc3   xyz3    2 2  3
# 1.3         1  abc    xyz    3 3  1
# 2.3         2 abc2   xyz2    3 6  2
# 3.3         3 abc3   xyz3    3 5  3

选项2:来自我的" splitstackshape"的merged.stack封装

library(splitstackshape)
merged.stack(mydf, var.stubs = "Q", sep = "var.stubs")
#    StudentID Name School .time_1 Q
# 1:         1  abc    xyz       1 1
# 2:         1  abc    xyz       2 5
# 3:         1  abc    xyz       3 3
# 4:         2 abc2   xyz2       1 2
# 5:         2 abc2   xyz2       2 4
# 6:         2 abc2   xyz2       3 6
# 7:         3 abc3   xyz3       1 1
# 8:         3 abc3   xyz3       2 2
# 9:         3 abc3   xyz3       3 5

为了好玩,第三种选择,再次在基地R:

cbind(mydf[!grepl("Q", names(mydf))], stack(mydf[grepl("Q", names(mydf))]))