楼主: jackylee2010
13657 19

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

  • 1关注
  • 13粉丝

已卖:152份资源

教授

46%

还不是VIP/贵宾

-

威望
0
论坛币
3341 个
通用积分
14.5414
学术水平
22 点
热心指数
31 点
信用等级
15 点
经验
2020 点
帖子
626
精华
0
在线时间
1878 小时
注册时间
2010-9-6
最后登录
2020-4-1

楼主
jackylee2010 发表于 2015-3-23 16:59:07 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
在stata下,有关于 长数据与宽数据转换的问题。
在r下,如何实现。

fy   company revenue profit
2010     Apple   65225  14013
2011     Apple  108249  25922
2012     Apple  156508  41733
2010    Google   29321   8505
2011    Google   37905   9737
2012    Google   50175  10737
2010 Microsoft   62484  18760
2011 Microsoft   69943  23150
2012 Microsoft   73723  16978


转换成 类似
                  revenue2010   revenue2011  revenue2012     profit2010 profit2011 profit2012
Apple
Google
Microsoft

这样排列的数据。 或是,这个转变成上面的 那种模式。
谢谢来。




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:宽数据 长数据 Microsoft Revenue company Microsoft Google company revenue profit

沙发
nieqiang110 学生认证  发表于 2015-3-23 21:18:29
reshape包
已有 1 人评分经验 收起 理由
李会超 + 80 精彩帖子

总评分: 经验 + 80   查看全部评分

藤椅
jackylee2010 发表于 2015-3-23 21:38:47
nieqiang110 发表于 2015-3-23 21:18
reshape包
可以演示一下吧。
另外,我的电脑reshape 总是安装不了。正在发愁中。

板凳
jackylee2010 发表于 2015-3-24 00:15:58
哪位大牛可以演示一下。谢谢来。

报纸
jackylee2010 发表于 2015-3-24 00:26:41
reshape(stats)
reshape()所属R语言包:stats

                                        Reshape Grouped Data
                                         重塑分组数据

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

This function reshapes a data frame between "wide" format with repeated measurements in separate columns of the same record and "long" format with the repeated measurements in separate records.
此功能重塑一个单独的列相同的记录,并在单独的记录重复测量长格式的重复测量之间的“宽”格式的数据框。


用法----------Usage----------


reshape(data, varying = NULL, v.names = NULL, timevar = "time",
        idvar = "id", ids = 1:NROW(data),
        times = seq_along(varying[[1]]),
        drop = NULL, direction, new.row.names = NULL,
        sep = ".",
        split = if (sep == "") {
            list(regexp = "[A-Za-z][0-9]", include = TRUE)
        } else {
            list(regexp = sep, include = FALSE, fixed = TRUE)}
        )




参数----------Arguments----------

参数:data
a data frame
一个数据框


参数:varying
names of sets of variables in the wide format that correspond to single variables in long format ("time-varying").  This is canonically a list of vectors of variable names, but it can optionally be a matrix of names, or a single vector of names.  In each case, the names can be replaced by indices which are interpreted as referring to names(data).  See "Details" for more details and options.
套在宽格式长格式的单变量(“时变”),对应的变量名。这是规范的向量变量名的名单,但它可以选择成为一个矩阵的名称或名称的单矢量。在每一种情况下,名称可以解释为指names(data)指数取代。看到更多的细节和选项的“详细资料”。


参数:v.names
names of variables in the long format that correspond to multiple variables in the wide format.  See "Details".
在长格式,对应的宽幅多个变量的变量名。见“详细资料”。


参数:timevar
the variable in long format that differentiates multiple records from the same group or individual.  If more than one record matches, the first will be taken.
在长格式的变量的区别来自同一团体或个人的多个记录。如果超过一个匹配的记录,首先将采取。


参数:idvar
Names of one or more variables in long format that identify multiple records from the same group/individual.  These variables may also be present in wide format.
在长格式中的一个或多个变量,找出多条记录,从同一组/个人的名称。这些变量也可能是目前在宽格式。


参数:ids
the values to use for a newly created idvar variable in long format.
使用一个新创建的idvar长格式的变量的值。


参数:times
the values to use for a newly created timevar variable in long format.  See "Details".
使用一个新创建的timevar长格式的变量的值。见“详细资料”。


参数:drop
a vector of names of variables to drop before reshaping.
变量名的向量下降重塑。


参数:direction
character string, either "wide" to reshape to wide format, or "long" to reshape to long format.
字符串,要么"wide"重塑宽幅,或"long"重塑长格式。


参数:new.row.names
logical; if TRUE and direction = "wide", create new row names in long format from the values of the id and time variables.
逻辑;如果TRUE和direction = "wide",id和time变量的值在长格式中创建新行名。


参数:sep
A character vector of length 1, indicating a separating character in the variable names in the wide format.  This is used for guessing v.names and times arguments based on the names in varying.  If sep == "", the split is just before the first numeral that follows an alphabetic character.  This is also used to create variable names when reshaping to wide format.
一个长度为1的特征向量,这表明在宽格式变量名的分隔符。这是用于猜测v.names和times根据varying的名字参数。如果sep == "",分裂前的第一个数字如下一个字母字符。这也可以用来重塑宽幅时创建变量名。


参数:split
A list with three components, regexp, include, and (optionally) fixed.  This allows an extended interface to variable name splitting.  See "Details".
与三个组成部分的列表,regexp,include,和(可选)fixed。这使得一个变量名分裂的扩展接口。见“详细资料”。


Details

详情----------Details----------

The arguments to this function are described in terms of longitudinal data, as that is the application motivating the functions.  A "wide" longitudinal dataset will have one record for each individual with some time-constant variables that occupy single columns and some time-varying variables that occupy a column for each time point.  In "long" format there will be multiple records for each individual, with some variables being constant across these records and others varying across the records.  A "long" format dataset also needs a "time" variable identifying which time point each record comes from and an "id" variable showing which records refer to the same person.
这个函数的参数描述纵向数据方面,因为这是激励功能的应用。 “宽”的纵向数据集,将有一些时间常数变量占据单个列和一些随时间变化的变量,占据了每个时间点列的每一个人的纪录之一。在长格式会有多个记录的每一个人,有些变量在整个记录这些记录和不同的人不断。一个“长”格式的数据集,还需要一个时间变量,确定哪个时间点每个记录来自ID变量显示的记录是指同一人。

If the data frame resulted from a previous reshape then the operation can be reversed simply by reshape(a).  The direction argument is optional and the other arguments are stored as attributes on the data frame.
如果数据框,导致从以前的reshape然后操作可逆转reshape(a)简单。 direction参数是可选的,其他参数都存储为数据框的属性。

If direction = "wide" and no varying or v.names arguments are supplied it is assumed that all variables except idvar and timevar are time-varying.  They are all expanded into multiple variables in wide format.
如果direction = "wide"没有varying或v.names参数提供假定,除了idvar和timevar是随时间变化的变量。他们都在扩大到宽幅多个变量。

If direction = "long" the varying argument can be a vector of column names (or a corresponding index).  The function will attempt to guess the v.names and times from these names.  The default is variable names like x.1, x.2, where sep = "." specifies to split at the dot and drop it from the name.  To have alphabetic followed by numeric times use sep = "".
如果direction = "long"varying参数可以是一个列名(或相应的指数)的向量。该函数将尝试猜测v.names和times从这些名字。默认的是变量名,像x.1x.2,其中sep = "."指定分割点,拖放它的名字。其次是数字时代的字母使用sep = ""。

Variable name splitting as described above is only attempted in the case where varying is an atomic vector, if it is a list or a matrix, v.names and times will generally need to be specified, although they will default to, respectively, the first variable name in each set, and sequential times.
如上所述的变量名分裂只试图在varying是一个原子的向量,如果它是一个列表或矩阵,v.names和times一般会需要指定情况虽然他们将默认分别在每一组的第一个变量的名字,连续倍。

Also, guessing is not attempted if v.names is given explicitly.  Notice that the order of variables in varying is like x.1,y.1,x.2,y.2.
此外,猜测,如果没有尝试v.names给出明确。请注意,在变量的顺序varying是x.1,y.1,x.2,y.2。

The split argument should not usually be necessary.  The split$regexp component is passed to either strsplit() or regexp(), where the latter is used if split$include is TRUE, in which case the splitting occurs after the first character of the matched string.  In the strsplit() case, the separator is not included in the result, and it is possible to specify fixed-string matching using split$fixed.
split参数通常不应该是必要的。 split$regexp组件传递给strsplit()或如果regexp(),后者用于split$include是TRUE,在这种情况下,发生分裂后的第一次字符匹配的字符串。分离器的在strsplit()的情况下,不包括在结果中,使用split$fixed,它可以指定固定字符串匹配。


值----------Value----------

The reshaped data frame with added attributes to simplify reshaping back to the original form.
重塑数据框添加的属性,以简化返回到原来的形式重塑。


参见----------See Also----------

stack, aperm; relist for reshaping the result of unlist.
stack,aperm;relist重塑unlist结果为。


举例----------Examples----------


summary(Indometh)
wide <- reshape(Indometh, v.names = "conc", idvar = "Subject",
                timevar = "time", direction = "wide")
wide

reshape(wide, direction = "long")
reshape(wide, idvar = "Subject", varying = list(2:12),
        v.names = "conc", direction = "long")

## times need not be numeric[#次不需要是数字]
df <- data.frame(id = rep(1:4, rep(2,4)),
                 visit = I(rep(c("Before","After"), 4)),
                 x = rnorm(4), y = runif(4))
df
reshape(df, timevar = "visit", idvar = "id", direction = "wide")
## warns that y is really varying[#Y是真的变警告]
reshape(df, timevar = "visit", idvar = "id", direction = "wide", v.names = "x")


##  unbalanced 'long' data leads to NA fill in 'wide' form[#不平衡“长”的数据,导致为NA填写在“宽”的形式]
df2 <- df[1:7, ]
df2
reshape(df2, timevar = "visit", idvar = "id", direction = "wide")

## Alternative regular expressions for guessing names[#另一种猜测的名字正则表达式]
df3 <- data.frame(id = 1:4, age = c(40,50,60,50), dose1 = c(1,2,1,2),
                  dose2 = c(2,1,2,1), dose4 = c(3,3,3,3))
reshape(df3, direction = "long", varying = 3:5, sep = "")


## an example that isn't longitudinal data[#一个例子,是不是纵向数据]
state.x77 <- as.data.frame(state.x77)
long <- reshape(state.x77, idvar = "state", ids = row.names(state.x77),
                times = names(state.x77), timevar = "Characteristic",
                varying = list(names(state.x77)), direction = "long")

reshape(long, direction = "wide")

reshape(long, direction = "wide", new.row.names = unique(long$state))

## multiple id variables[#多个ID变量]
df3 <- data.frame(school = rep(1:3, each = 4), class = rep(9:10, 6),
                  time = rep(c(1,1,2,2), 3), score = rnorm(12))
wide <- reshape(df3, idvar = c("school","class"), direction = "wide")
wide
## transform back[#转换回]
reshape(wide)

地板
king64 发表于 2015-3-24 13:58:48
> z
     X   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

> y <- cast(z[,1:3],company ~ X)
Using revenue as value column.  Use the value argument to cast to override this choice
> y
    company  2010   2011   2012
1     Apple 65225 108249 156508
2    Google 29321  37905  50175
3 Microsoft 62484  69943  73723

> y2 <- cast(z,company ~ X)
Using profit as value column.  Use the value argument to cast to override this choice
> y2
    company  2010  2011  2012
1     Apple 14013 25922 41733
2    Google  8505  9737 10737
3 Microsoft 18760 23150 16978
> cbind(y,y2)
    company  2010   2011   2012   company  2010  2011  2012
1     Apple 65225 108249 156508     Apple 14013 25922 41733
2    Google 29321  37905  50175    Google  8505  9737 10737
3 Microsoft 62484  69943  73723 Microsoft 18760 23150 16978
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
jzbd + 5 + 5 + 5 + 5 精彩帖子

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

7
jzbd 发表于 2015-3-24 18:50:16
king64 发表于 2015-3-24 13:58
> z
     X   company revenue profit
1 2010     Apple   65225  14013
可以再反向操作一次不??

8
jzbd 发表于 2015-3-24 18:54:17
company  2010   2011   2012   company  2010  2011  2012
1     Apple 65225 108249 156508     Apple 14013 25922 41733
2    Google 29321  37905  50175    Google  8505  9737 10737
3 Microsoft 62484  69943  73723 Microsoft 18760 23150 16978

变成
   X   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


9
king64 发表于 2015-3-25 08:45:22
jzbd 发表于 2015-3-24 18:54
company  2010   2011   2012   company  2010  2011  2012
1     Apple 65225 108249 156508     Apple 1 ...
> m <- cbind(y,y2)
> m
    company  2010   2011   2012   company  2010  2011  2012
1     Apple 65225 108249 156508     Apple 14013 25922 41733
2    Google 29321  37905  50175    Google  8505  9737 10737
3 Microsoft 62484  69943  73723 Microsoft 18760 23150 16978
> m <- m[,-5]
> m
    company  2010   2011   2012 2010.1 2011.1 2012.1
1     Apple 65225 108249 156508  14013  25922  41733
2    Google 29321  37905  50175   8505   9737  10737
3 Microsoft 62484  69943  73723  18760  23150  16978

> n <-melt(m)
Using company as id variables
> n
     company variable  value
1      Apple     2010  65225
2     Google     2010  29321
3  Microsoft     2010  62484
4      Apple     2011 108249
5     Google     2011  37905
6  Microsoft     2011  69943
7      Apple     2012 156508
8     Google     2012  50175
9  Microsoft     2012  73723
10     Apple   2010.1  14013
11    Google   2010.1   8505
12 Microsoft   2010.1  18760
13     Apple   2011.1  25922
14    Google   2011.1   9737
15 Microsoft   2011.1  23150
16     Apple   2012.1  41733
17    Google   2012.1  10737
18 Microsoft   2012.1  16978
> n1 <- n[1:9,]
> n1[order(n1$company),]
    company variable  value
1     Apple     2010  65225
4     Apple     2011 108249
7     Apple     2012 156508
2    Google     2010  29321
5    Google     2011  37905
8    Google     2012  50175
3 Microsoft     2010  62484
6 Microsoft     2011  69943
9 Microsoft     2012  73723
> n2 <- n[10:18,]
> n2[order(n2$company),]
     company variable value
10     Apple   2010.1 14013
13     Apple   2011.1 25922
16     Apple   2012.1 41733
11    Google   2010.1  8505
14    Google   2011.1  9737
17    Google   2012.1 10737
12 Microsoft   2010.1 18760
15 Microsoft   2011.1 23150
18 Microsoft   2012.1 16978
已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
admin_kefu + 30 热心帮助其他会员
jzbd + 5 + 5 + 5 + 5 精彩帖子

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

10
king64 发表于 2015-3-25 09:27:16
> n1 <- n[1:9,]
> n1[order(n1$company),]
    company variable  value
1     Apple     2010  65225
4     Apple     2011 108249
7     Apple     2012 156508
2    Google     2010  29321
5    Google     2011  37905
8    Google     2012  50175
3 Microsoft     2010  62484
6 Microsoft     2011  69943
9 Microsoft     2012  73723
> n2 <- n[10:18,]
> n2[order(n2$company),]
     company variable value
10     Apple   2010.1 14013
13     Apple   2011.1 25922
16     Apple   2012.1 41733
11    Google   2010.1  8505
14    Google   2011.1  9737
17    Google   2012.1 10737
12 Microsoft   2010.1 18760
15 Microsoft   2011.1 23150
18 Microsoft   2012.1 16978
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
jzbd + 5 + 5 + 5 + 5 精彩帖子

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

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

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