2017-05-30 83 views
0

我开始使用tidyr和dplyr。我有以下的数据帧:重新格式化数据

      email Assignment Stage Grade 
1      [email protected] course final 86.28 
2      [email protected] course first 68.87 
3      [email protected] course resub 38.06 
4      [email protected] course final 77.41 
... 

我想这样重组此,基于阶段(第一,RESUB或最终)创建从一个等级列对应于第一阶段的值三列的值

      email Assignment first resub final 
1      [email protected] course 100.0 100.0 100.0 
2      [email protected] course 100.0 100.0 100.0 
3      [email protected] course 100.0 100.0 100.0 
4      [email protected] course 100.0 100.0 100.0 

(数据显然不是因为剪切/粘贴的匹配。)

我很困惑,我需要一个单独的()函数,但如何?谢谢!

回答

1

tidyr的spread()函数应该为您提供所需的结果。

email <- c("[email protected]","[email protected]","[email protected]","[email protected]") 
Assignment <- rep("course",4) 
Stage <- c("final","first","resub","final") 
Grade <- c(86.28,68.87,38.06,77.41) 

df <- data.frame(email,Assignment,Stage,Grade,stringsAsFactors = FALSE) 

df <- df %>% 
     spread(Stage, Grade)