将长格式 R 数据分割成多列
我收到了一些已合并为长格式的数据,但我需要将其转换为某种可交付成果的格式。我已经修改了数据框和列表选项,但似乎无法找到一种方法将我拥有的数据转换为我需要的输出形式。任何想法和解决方案都值得赞赏。
如果所需的输出形式对于 R 来说似乎很奇怪,那是因为其他人会在 Excel 中打开结果数据进行进一步研究。所以我会将最终数据保存为 csv 或 Excel 文件。所需形式的完整数据将有 40 行(+标题)和 110 列(55 个学生和分数对)。
以下是我的长格式数据的示例代码:
Class | Student | Score |
---|---|---|
1 | a | 0.4977 |
1 | b | 0.7176 |
1 | c | 0.9919 |
1 | d | 0.3800 |
1 | e | 0.7774 |
2 | f | 0.9347 |
2 | g | 0.2121 |
2 | h | 0.6517 |
2 | i | 0.1256 |
2 | j | 0.2672 |
3 | k | 0.3861 |
3 | l | 0.0134 |
3 | m | 0.3824 |
3 | n | 0.8697 |
3 | o | 0.3403 |
以下是我需要如何显示最终数据的示例:
class_1_student | class_1_score | class_2_student | class_2_score | class_3_student | class_3_score |
---|---|---|---|---|---|
a | 0.4977 | f | 0.9347 | k | 0.3861 |
b | 0.7176 | g | 0.2121 | l | 0.0134 |
c | 0.9919 | h | 0.6517 | m | 0.3824 |
d | 0.3800 | i | 0.1256 | n | 0.8697 |
e | 0.7774 | j | 0.2672 | o | 0.3403 |
这是生成示例长形式和所需形式的 R 代码数据:
set.seed(1)
d <- data.frame(
class=c(rep(1,5), rep(2,5), rep(3,5)),
student=c(letters[1:5], letters[6:10], letters[11:15]),
score=round(runif(15, 0, 1),4)
)
d2 <- data.frame(
class_1_student = d[1:5,2],
class_1_score = d[1:5,3],
class_2_student = d[6:10,2],
class_2_score = d[6:10,3],
class_3_student = d[11:15,2],
class_3_score = d[11:15,3]
)
如果有帮助的话,我还将学生和分数数据放在单独的矩阵中(每个学生 1 行,每个班级 1 列),我可以用它来帮助生成最终数据。
I've been given some data that I've combined into long form, but I need to get it into a certain format for a deliverable. I've tinkered with dataframe and list options and cannot seem to find a way to get the data I have into the output form I need. Any thoughts and solutions are appreciated.
If the desired output form seems odd for R, it is because other people will open the resulting data in Excel for additional study. So I will save the final data as a csv or Excel file. The full data in the desired form will have 40 rows (+header) and 110 columns (55 student and score pairs).
Here is example code for my long form data:
class | student | score |
---|---|---|
1 | a | 0.4977 |
1 | b | 0.7176 |
1 | c | 0.9919 |
1 | d | 0.3800 |
1 | e | 0.7774 |
2 | f | 0.9347 |
2 | g | 0.2121 |
2 | h | 0.6517 |
2 | i | 0.1256 |
2 | j | 0.2672 |
3 | k | 0.3861 |
3 | l | 0.0134 |
3 | m | 0.3824 |
3 | n | 0.8697 |
3 | o | 0.3403 |
Here is an example of how I need the final data to appear:
class_1_student | class_1_score | class_2_student | class_2_score | class_3_student | class_3_score |
---|---|---|---|---|---|
a | 0.4977 | f | 0.9347 | k | 0.3861 |
b | 0.7176 | g | 0.2121 | l | 0.0134 |
c | 0.9919 | h | 0.6517 | m | 0.3824 |
d | 0.3800 | i | 0.1256 | n | 0.8697 |
e | 0.7774 | j | 0.2672 | o | 0.3403 |
Here is R code to generate the sample long form and desired form data:
set.seed(1)
d <- data.frame(
class=c(rep(1,5), rep(2,5), rep(3,5)),
student=c(letters[1:5], letters[6:10], letters[11:15]),
score=round(runif(15, 0, 1),4)
)
d2 <- data.frame(
class_1_student = d[1:5,2],
class_1_score = d[1:5,3],
class_2_student = d[6:10,2],
class_2_score = d[6:10,3],
class_3_student = d[11:15,2],
class_3_score = d[11:15,3]
)
If it's helpful, I also have the student and score data in separate matrices (1 row per student and 1 column per class) that I could use to help generate the final data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只拆分数据:
仅当组大小相等时,列绑定才有效。
You can just split data:
Column binding will work only if the groups are of equal sizes.