楼主: jackylee2010
13658 19

[问答] 长数据与宽数据的转换 [推广有奖]

11
蓝色 发表于 2015-3-25 10:02:54
> data.long
    fy   company revenue profit
1 2010     Apple   65225  14013
2 2011     Apple  108249  25922
3 2012     Apple  156508  41733
4 2010    Google   29321   8505
5 2011    Google   37905   9737
6 2012    Google   50175  10737
7 2010 Microsoft   62484  18760
8 2011 Microsoft   69943  23150
9 2012 Microsoft   73723  16978

> data.wide <-reshape(data.long,
+                      idvar   = "company",
+                      timevar = "fy",
+                      times   = c(seq(from = 2010, to = 2012, by = 1)),
+                      v.names = c("revenue", "profit"),
+                      direction = "wide")



> data.wide
    company revenue.2010 profit.2010 revenue.2011 profit.2011 revenue.2012 profit.2012
1     Apple        65225       14013       108249       25922       156508       41733
4    Google        29321        8505        37905        9737        50175       10737
7 Microsoft        62484       18760        69943       23150        73723       16978
>
已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
admin_kefu + 20 热心帮助其他会员
jzbd + 5 + 5 + 5 + 5 精彩帖子

总评分: 论坛币 + 25  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

12
jzbd 发表于 2015-3-25 15:12:19
rm(list=ls(all=TRUE))

x<-c(2010,2011,2012,2010,2011,2012,2010,2011,2012)
company<-c("apple","apple","apple","google","google","google","microsoft",
"microsoft","microsoft")
revenue<-c(65225,108249,156508,29321,37905,50175,62484,69943 ,73723)
profit<-c(14013,25922,41733,8505,9737,10737,18760,23150,16978)

z<-data.frame(x,company,revenue,profit)
z

z_wide <- reshape(z, v.names = c("revenue","profit"), idvar = "company",
             timevar = "x", direction = "wide")

z_wide

##############

z_long<- reshape(z_wide, idvar = "company",timevar = "x",
        direction = "long")

z_long

z_long[order(z_long$company,z_long$x),]

13
jzbd 发表于 2015-3-25 15:17:37
蓝色 发表于 2015-3-25 10:02
> data.long
    fy   company revenue profit
1 2010     Apple   65225  14013
楼主,
长数据 转换陈 宽数据,已经没有问题了。 用reshape函数,或是, reshape 包,操作都还可以。
不过,在短数据里面,
列的排序为
company revenue.2010 profit.2010 revenue.2011 profit.2011 revenue.2012 profit.2012
   如果把列的排序修改为:
company revenue.2010    revenue.2011   revenue.2012  profit.2010  profit.2011  profit.2012
就更好。我试了一下,可以用 order 命令。类似。 data.wide[order(colnames( data.wide))], 但是,这样有一些麻烦。需要先把,company放在一般,等排好序,再把 company放进来。
,有什么好的操作吗?

14
xucaifeng66 发表于 2015-3-25 15:26:10
可以试试t()这个函数

15
jzbd 发表于 2015-3-25 18:18:19
t()是转置。不是一回事。
这个要比转置复杂一些。

16
king64 发表于 2015-3-26 09:43:47
jzbd 发表于 2015-3-25 15:17
楼主,
长数据 转换陈 宽数据,已经没有问题了。 用reshape函数,或是, reshape 包,操作都还可以。
不 ...
分两次reshape比较好吧?
z_wide1 <- reshape(z[,-4], v.names = c("revenue"), idvar = "company",timevar = "x", direction = "wide")
z_wide2 <- reshape(z[,-3], v.names = c("profit"), idvar = "company",timevar = "x", direction = "wide")

再把,z_wide1和z_wide2 bind在一起。这样也不麻烦。
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
jzbd + 5 + 5 + 5 + 5 精彩帖子

总评分: 论坛币 + 5  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

17
jzbd 发表于 2015-3-26 10:53:48
king64 发表于 2015-3-26 09:43
分两次reshape比较好吧?
z_wide1
这个主意不错。
但是,还是感觉stata 在处理类似的问题上,要方便的多。

18
千原夏树 发表于 2016-1-4 17:59:46
king64 发表于 2015-3-24 13:58
> z
     X   company revenue profit
1 2010     Apple   65225  14013
这是我照着做的,数据怎么全变成1和0了?如图
图片

19
ryoeng 在职认证  发表于 2016-1-5 21:10:47
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽

20
coperLil 发表于 2018-4-3 10:51:28
> x.melt <- melt(x)
Using fy, company as id variables
> cast(x.melt,company~variable+fy)
    company revenue_2010 revenue_2011 revenue_2012 profit_2010 profit_2011 profit_2012
1     Apple        65225       108249       156508       14013       25922       41733
2    Google        29321        37905        50175        8505        9737       10737
3 Microsoft        62484        69943        73723       18760       23150       16978

应该可以的。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 10:59