r - 是否有一个 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
反正有没有将数据从长到宽重塑?
谢谢!
解决方案
在基础 R 中,您可以使用以下reshape
功能:
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
推荐阅读
- c# - 通过 LINQ 对列表中的特定值进行排序
- python - 找到一个子字符串并打印它直到特定字符
- reactjs - React 历史不能在现场工作,但它可以在本地主机上工作
- pdf - 无法在 Delphi XE3 中添加 Acrobat PDF 控件
- html - 内容正在切割
- ios - iOS 14 UIDevice.current.setValue(xxx, forKey: "orientation") 不起作用
- javascript - 如何在jquery php代码中检查多个复选框
- sql-server - SQL Server 代理定期停止执行 SSIS 作业
- c# - 当操作以对 Challenge(AuthenticationProperties, string[]) 的往返结束时未保存 Cookie
- java - 如何将主体传递给后端 EJB