如何在R或Excel中重塑数据框?

4
这里是获取样本数据集的代码:
set.seed(0)
practice <- matrix(sample(1:100, 20), ncol = 2)
data <- as.data.frame(practice)
data <- cbind( lob = sprintf("objective%d", rep(1:2,each=5)), data)
data <- cbind( student = sprintf("student%d", rep(1:5,2)), data)
names(data) <- c("student", "learning objective","attempt", "score")
data[-8,]

数据长这样:
    student learning objective attempt score
1  student1         objective1      90     6
2  student2         objective1      27    19
3  student3         objective1      37    16
4  student4         objective1      56    60
5  student5         objective1      88    34
6  student1         objective2      20    66
7  student2         objective2      85    42
9  student4         objective2      61    82
10 student5         objective2      58    31

What I want is:

    student       objective1         objective2 
                 attempt  score     attempt score
1  student1         90     6          20      66
2  student2         27    19          85      42
3  student3         ...                0       0
4  student4         ...                  ...
5  student5         ...                  ...

有 70 个学习目标,因此仅复制和粘贴尝试和分数将是枯燥无味的,我想知道是否有更好的方法清理数据。
R: 我试图使用 R 中的 melt 函数获取新数据,但它效果不佳。一些学生的得分缺失,例如在这种情况下的student3,而且学生姓名未列出,因此我不能只使用 cbind 来合并得分。
Excel: 由于姓名缺失,所有 70 个学习目标的相应行都需要进行 VLOOKUP 检查:
(=VLOOKUP($C7,'0learning.csv'!$B$372:$G$395,5,0)
(=VLOOKUP($C7,'0learning.csv'!$B$372:$G$395,6,0)

有更好的方法吗?
1个回答

4
我们可以使用v1.9.5版本的开发版data.table,它可以处理多个value.var列,并将“长”格式重塑为“宽”格式。 安装说明在这里。请保留HTML标记。
 library(data.table)#v1.9.5+
 names(data)[2] <- 'objective'
 dcast(setDT(data), student~objective, value.var=c('attempt', 'score'))
 #    student attempt_objective1 attempt_objective2 score_objective1
 #1: student1                 90                 20                6
 #2: student2                 27                 85               19
 #3: student3                 37                 96               16
 #4: student4                 56                 61               60
 #5: student5                 88                 58               34
 #    score_objective2
 #1:               66
 #2:               42
 #3:               87
 #4:               82
 #5:               31

或者使用来自base Rreshape

 reshape(data, idvar='student', timevar='objective', direction='wide')
 #  student attempt.objective1 score.objective1 attempt.objective2
 #  1 student1                 90                6                 20
 #  2 student2                 27               19                 85
 #  3 student3                 37               16                 96
 #  4 student4                 56               60                 61
 #  5 student5                 88               34                 58
 #    score.objective2
 #  1               66
 #  2               42
 #  3               87
 #  4               82
 #  5               31

谢谢,但是似乎这两行代码都有错误.. 1: > names(data)[2] <-'objective' 警告信息:... 2: > dcast(setDT(data),student~objective,value.var=c('attempt','score')) Error in .subset2(x,i,exact = exact) : subscript out of bounds - SongTianyang
@SongTianyang 你是否正在使用data.table的开发版本? - akrun
@SongTianyang 我添加了一个“基本R”版本,如果你没有data.table的开发版本,它应该可以工作。 - akrun
谢谢!我已经下载了这个包,但是默认版本是1.9.4。 - SongTianyang
改变形状的函数有效了!非常感谢你! - SongTianyang
@SongTianyang 在CRAN中,它是1.9.4版本。如果您查看我在帖子中提到的链接,它会让您下载和安装开发版本。 - akrun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接