如何将 R 数据从长格式转换为宽格式 [英] How to transform R data from long-ish to wide-ish

查看:46
本文介绍了如何将 R 数据从长格式转换为宽格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R 尝试将我的数据框从long-ish"转换为wide-ish",但我徒劳地寻找使用结构与我的数据相似的数据的答案.这是我的数据:

I am using R to try to transform my data frame from "long-ish" to "wide-ish" and I have searched in vain for an answer that uses data similar in structure to mine. Here are my data:

| ID   | NAME | V1    |  V2 |   V3 |
|------|------|-------|----:|-----:|
| 1001 | Bob  | Red   | 302 | 0.50 |
| 1001 | Bob  | Blue  | 737 | 0.50 |
| 1002 | Jim  | Red   | 432 | 0.14 |
| 1002 | Jim  | Blue  | 643 | 0.60 |
| 1002 | Jim  | Green |  34 | 0.46 |
| 1006 | Dan  | Red   | 876 | 1.25 |

这就是我希望最终数据(宽)的外观:

And this is how I would like the final data (wide) to look:

| ID   | NAME | V2.Red | V2.Blue | V2.Green | V3.Red | V3.Blue | V3.Green |
|------|------|-------:|--------:|---------:|-------:|--------:|---------:|
| 1001 | Bob  |    302 |     737 |      N/A |   0.50 |    0.50 |      N/A |
| 1002 | Jim  |    432 |     643 |       34 |   0.14 |    0.60 |     0.46 |
| 1006 | Dan  |    876 |     N/A |      N/A |   1.25 |     N/A |      N/A |

所以,基本上,我将所有相同的 ID 行合并为一行(带有伴随的 NAME),以便总行数等于唯一 ID 值的数量.
然后我使用 V1 的唯一值来创建尽可能多的列,因为 V1 中的唯一值乘以额外变量"的数量——V2、V3.(我有更多 V2 和 V3 类型的变量.

So, basically, I'm collapsing all of the same ID rows into one row (with accompanying NAME) so that the total number of rows is equal to the number of unique ID values.
I am then using the unique values of V1 to create as many columns as there are unique values in V1 times the number of "extra variables"--V2, V3. (I have many more variables of the V2 and V3 type.

提前致谢!

推荐答案

为了完整起见,这里是一个 data.table 解决方案

For completeness sake, here is a data.table solution

library( data.table )
dcast( setDT(df), ID + NAME ~ V1, value.var = c("V2","V3"), sep = "." )

#      ID NAME V2.Blue V2.Green V2.Red V3.Blue V3.Green V3.Red
# 1: 1001  Bob     737       NA    302     0.5       NA   0.50
# 2: 1002  Jim     643       34    432     0.6     0.46   0.14
# 3: 1006  Dan      NA       NA    876      NA       NA   1.25

这篇关于如何将 R 数据从长格式转换为宽格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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