Reshape Data Long to Wide - 了解 reshape 参数 [英] Reshape Data Long to Wide - understanding reshape parameters

查看:31
本文介绍了Reshape Data Long to Wide - 了解 reshape 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长格式数据框狗,我正在尝试使用 reshape() 函数将其重新格式化为宽格式.目前看起来是这样的:

I have a long format dataframe dogs that I'm trying to reformat to wide using the reshape() function. It currently looks like so:

dogid  month  year  trainingtype  home  school  timeincomp
12345  1      2014  1             1     1       340
12345  2      2014  1             1     1       360
31323  12     2015  2             7     3       440
31323  1      2014  1             7     3       500
31323  2      2014  1             7     3       520

dogid 列是一组 id,每只狗一个.12 个月的月份列从 1 到 12 变化,2014 到 2015 年的年份变化.Trainingtype 从 1 到 2 变化.每只狗的每个月-年-trainingtype 组合都有一个 timeincomp 值,因此每只狗有 48 个条目.家庭和学校从 1 到 8 不等,并且每只狗都是不变的(同一只狗的每个条目都有相同的学校和家庭).参加比赛的时间是我的反应变量.

The dogid column is a bunch of ids, one for each dog. The month column varies for 1 to 12 for the 12 months, and year from 2014 to 2015. Trainingtype varies for 1 to 2. Each dog has a timeincomp value for every month-year-trainingtype combination, so 48 entries per dog. Home and school vary from 1-8 and are constant per dog (every entry for the same dog has the same school and home). Time in comp is my response variable.

我希望我的桌子看起来像这样:

I would like my table to look like so:

dogid  home  school  month1year2014trainingtype1  month2year2014trainingtype1
12345  1     1       340                          360
31323  7     3       500                          520

等等.(每个月-年-培训类型组合的列)

etc. (with columns for each month-year-trainingtype combination)

我应该在 reshape 中使用哪些参数来实现这一点?

What parameters should I use in reshape to achieve this?

推荐答案

您可以使用 reshape2 包中的 dcast 函数.更容易理解.公式左边是长的,右边是宽的.

You can use the function dcast from package reshape2. It's easier to understand. The left side of the formula is the one that stays long, while the right side is the one that goes wide.

fun.aggregate 是在每个案例超过 1 个数字的情况下应用的函数.如果确定没有重复的情况,可以使用 meansum

The fun.aggregate is the function to apply in case that there is more than 1 number per case. If you're sure you don't have repeated cases, you can use mean or sum

dcast(data, formula= dogid + home + school ~ month + year + trainingtype,
value.var = 'timeincomp',
fun.aggregate = sum)

我希望它有效:

  dogid home school 1_2014_1 2_2014_1 12_2015_2
1 12345    1      1      340      360         0
2 31323    7      3      500      520       440

这篇关于Reshape Data Long to Wide - 了解 reshape 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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