是否有R函数可以从长到宽重塑这些数据? [英] Is there an R function to reshape this data from long to wide?

查看:46
本文介绍了是否有R函数可以从长到宽重塑这些数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在的数据外观:

Coach ID | Student | score |
---------------------------------
1         | A      | 8     |
1         | B      | 3     |  
2         | A      | 5     |
2         | B      | 4     |
2         | C      | 7     |

看起来像这样:

Coach ID | Student | score | student_2|score_2| student_3|score_3
------------------------------------------------------------------
1         | A      | 8     | B        | 3     |   
2         | A      | 5     | B        | 4     | C        | 7

是否有从长到宽的数据重塑形式?

Is there anyway to reshape data from long to wide?

谢谢!

推荐答案

在base R中,您可以使用 reshape 函数:

In base R, you could use the reshape function:

reshape(transform(df,time = as.numeric(factor(Student))),idvar = "CoachID",dir = "wide",sep="_")
  CoachID Student_1 score_1 Student_2 score_2 Student_3 score_3
1       1         A       8         B       3      <NA>      NA
3       2         A       5         B       4         C       7

这篇关于是否有R函数可以从长到宽重塑这些数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆