重塑数据框以创建重复变量

时间:2014-11-28 14:56:27

标签: r dataframe reshape reshape2

我有一个记录人员资格的数据集。每个人有几行数据,变量为宽格式。我需要进一步“扩展”它,以便我为数据中的每个人创建一行,并将变量重复为列。你猜对了 - 数据需要进入电子表格模板。

每人最多10行,但没有指定的最小值。

以下是当前形式的数据的简化示例:

current <- structure(list(id = c("Bob", "Bob", "Bob", "Bob", "Jim", "Jim", 
"Jim", "Jim"), awarding.body = c("SQA", "SQA", "SQA", "SQA", 
"SQA", "SQA", "SQA", "SQA"), qual.type = c("HIGHER GRADE", "HIGHER GRADE", 
"STANDARD GRADE", "STANDARD GRADE", "HIGHER GRADE", "HIGHER GRADE", 
"STANDARD GRADE", "STANDARD GRADE"), year.awarded = c(1998L, 
1998L, 1996L, 1996L, 1999L, 1999L, 1997L, 1997L), band = c("A", 
"A", "B", "B", "B", "B", "A", "B"), subject = c("Mathematics", 
"Chemistry", "French", "Physics", "Fine Art", "Geography", "Craft & Design", 
"French")), .Names = c("id", "awarding.body", "qual.type", "year.awarded", 
"band", "subject"), class = "data.frame", row.names = c(NA, -8L
))

以下是我需要数据的方式

desired <- structure(list(id = c("Bob", "Jim"), awarding.body.1 = c("SQA", 
"SQA"), qual.type.1 = c("HIGHER GRADE", "HIGHER GRADE"), year.awarded.1 = 1998:1999, 
    band.1 = c("A", "B"), subject.1 = c("Mathematics", "Fine Art"
    ), awarding.body.2 = c("SQA", "SQA"), qual.type.2 = c("HIGHER GRADE", 
    "HIGHER GRADE"), year.awarded.2 = 1998:1999, band.2 = c("A", 
    "B"), subject.2 = c("Chemistry", "Geography"), awarding.body.3 = c("SQA", 
    "SQA"), qual.type.3 = c("STANDARD GRADE", "STANDARD GRADE"
    ), year.awarded.3 = 1996:1997, band.3 = c("B", "A"), subject.3 = c("French", 
    "Craft & Design"), awarding.body.4 = c("SQA", "SQA"), qual.type.4 = c("STANDARD GRADE", 
    "STANDARD GRADE"), year.awarded.4 = 1996:1997, band.4 = c("B", 
    "B"), subject.4 = c("Physics", "French")), .Names = c("id", 
"awarding.body.1", "qual.type.1", "year.awarded.1", "band.1", 
"subject.1", "awarding.body.2", "qual.type.2", "year.awarded.2", 
"band.2", "subject.2", "awarding.body.3", "qual.type.3", "year.awarded.3", 
"band.3", "subject.3", "awarding.body.4", "qual.type.4", "year.awarded.4", 
"band.4", "subject.4"), class = "data.frame", row.names = c(NA, 
-2L))

我用Reshape2包尝试过各种各样的东西,但我不认为这是一个典型的重塑问题?我在这里看了各种重塑问题,但还没有找到解决方案。

建议非常感谢。

1 个答案:

答案 0 :(得分:1)

尝试

current1 <- transform(current, indx=ave(seq_along(id), id, FUN=seq_along))
desired1 <- reshape(current1, idvar='id', timevar='indx', direction='wide')
row.names(desired1) <- NULL
attr(desired1, 'reshapeWide') <- NULL
all.equal(desired1, desired)
#[1] TRUE