在 R 中使用从宽到长的 Reshape [英] Using Reshape from wide to long in R

查看:66
本文介绍了在 R 中使用从宽到长的 Reshape的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 R 中将数据从宽变长.

I am trying to reshape Data from wide to long in R.

我的宽格式数据如下所示:

my data in wide format looks like this:

我有以下数据矩阵:在行中我有不同的公司,在列中有不同年份的不同变量.(earnings_2012、earnings_2011、earnings_2010、...、tot_assets_2012、tot_assets_2011,等等.

I have the following Data-Matrix: in the rows i have the different companies, and in the columns in have different variables from different years. (earnings_2012, earnings_2011, earnings_2010,...,tot_assets_2012,tot_assets_2011, and so on.

我想将其重新排列为长格式:

I would like to rearrange this into a long format:

只有这些列:公司、年份、变量(收入、tot_assets、..)

having only these columns: company, year, variables (earnings,tot_assets,..)

我已经尝试了好几天了.我还咨询了http://www.ats.ucla.edu/stat/r/faq/reshape.htm 寻求帮助,但我无法理解他们如何重塑数据以及如何分离年份.

I've been trying for days now. I also consulted http://www.ats.ucla.edu/stat/r/faq/reshape.htm for help but i could not grasp how they reshaped their data and how to detach the year.

预先感谢您的帮助.

问候

格里蒂

PS:由于我是 R 的新手,并且 R 本身提供的帮助似乎相当技术性,我感谢任何关于非常简单介绍的提示.

PS: Since I am new to R and the provided help in R itself seems to be rather technical I appreciate any hints for a very simple introduction.

推荐答案

以下是三个示例(以及一些我认为代表您所描述内容的示例数据).

Here are three examples (along with some sample data that I think is representative of what you described).

这是示例数据:

set.seed(1)
mydf <- data.frame(
  company = LETTERS[1:4],
  earnings_2012 = runif(4),
  earnings_2011 = runif(4),
  earnings_2010 = runif(4),
  assets_2012 = runif(4),
  assets_2011 = runif(4),
  assets_2010 = runif(4)
)

mydf
#   company earnings_2012 earnings_2011 earnings_2010 assets_2012 assets_2011 assets_2010
# 1       A     0.2655087     0.2016819    0.62911404   0.6870228   0.7176185   0.9347052
# 2       B     0.3721239     0.8983897    0.06178627   0.3841037   0.9919061   0.2121425
# 3       C     0.5728534     0.9446753    0.20597457   0.7698414   0.3800352   0.6516738
# 4       D     0.9082078     0.6607978    0.17655675   0.4976992   0.7774452   0.1255551

选项 1:重塑

一个限制是它不能处理不平衡"的数据集(例如,如果您的数据中没有assets_2010",这将不起作用).

reshape(mydf, direction = "long", idvar="company", 
        varying = 2:ncol(mydf), sep = "_")
#        company time   earnings    assets
# A.2012       A 2012 0.26550866 0.6870228
# B.2012       B 2012 0.37212390 0.3841037
# C.2012       C 2012 0.57285336 0.7698414
# D.2012       D 2012 0.90820779 0.4976992
# A.2011       A 2011 0.20168193 0.7176185
# B.2011       B 2011 0.89838968 0.9919061
# C.2011       C 2011 0.94467527 0.3800352
# D.2011       D 2011 0.66079779 0.7774452
# A.2010       A 2010 0.62911404 0.9347052
# B.2010       B 2010 0.06178627 0.2121425
# C.2010       C 2010 0.20597457 0.6516738
# D.2010       D 2010 0.17655675 0.1255551

选项 2:reshape2"包

因其语法而广受欢迎.在它可以工作之前需要一些处理,因为需要拆分列名以便我们获得这种双宽"类型的数据.能够处理不平衡的数据,但如果您的不同列具有不同的列类型(数字、字符、因子),则不会是最好的.

library(reshape2)
dfL <- melt(mydf, id.vars="company")
dfL <- cbind(dfL, colsplit(dfL$variable, "_", c("var", "year")))
dcast(dfL, company + year ~ var, value.var="value")
#    company year    assets   earnings
# 1        A 2010 0.9347052 0.62911404
# 2        A 2011 0.7176185 0.20168193
# 3        A 2012 0.6870228 0.26550866
# 4        B 2010 0.2121425 0.06178627
# 5        B 2011 0.9919061 0.89838968
# 6        B 2012 0.3841037 0.37212390
# 7        C 2010 0.6516738 0.20597457
# 8        C 2011 0.3800352 0.94467527
# 9        C 2012 0.7698414 0.57285336
# 10       D 2010 0.1255551 0.17655675
# 11       D 2011 0.7774452 0.66079779
# 12       D 2012 0.4976992 0.90820779

选项 3:merged.stack 来自splitstackshape"

merged.stack 来自我的splitstackshape"包的语法非常简单,如果您需要以这种双宽"类型的结构结束,应该会非常快.它的创建是为了能够处理不平衡的数据,并且由于它单独处理列,因此在转换列类型时不会出现问题.

Option 3: merged.stack from "splitstackshape"

merged.stack from my "splitstackshape" package has pretty straightforward syntax and should be pretty fast if you need to end up with this "double-wide" type of structure. It was created to be able to handle unbalanced data and since it treats columns separately, won't have problems with converting column types.

library(splitstackshape)
merged.stack(mydf, id.vars="company", 
             var.stubs=c("earnings", "assets"), sep = "_")
#     company .time_1   earnings    assets
#  1:       A    2010 0.62911404 0.9347052
#  2:       A    2011 0.20168193 0.7176185
#  3:       A    2012 0.26550866 0.6870228
#  4:       B    2010 0.06178627 0.2121425
#  5:       B    2011 0.89838968 0.9919061
#  6:       B    2012 0.37212390 0.3841037
#  7:       C    2010 0.20597457 0.6516738
#  8:       C    2011 0.94467527 0.3800352
#  9:       C    2012 0.57285336 0.7698414
# 10:       D    2010 0.17655675 0.1255551
# 11:       D    2011 0.66079779 0.7774452
# 12:       D    2012 0.90820779 0.4976992

这篇关于在 R 中使用从宽到长的 Reshape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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