加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

R语言cast函数数据分类的使用方法

(2017-01-26 10:28:58)
标签:

财经

分类:
library(reshape)
set.seed(20)
gcz<-data.frame(subj=rep(1:4,rep(3,4)),time=rep(1:3),x=rnorm(12),y=rnorm(12))
mgcz=melt(gcz,measure=c('x','y'))
cast(subj~variable+time,data=mgcz)
  subj        x_1         x_2        x_3         y_1       y_2         y_3
1    1  1.1626853 -0.58592447  1.7854650 -0.62812676 1.3232209 -1.52135057
2    2 -1.3325937 -0.44656677  0.5696061 -0.43742787 0.9705776  0.02822264
3    3 -2.8897176 -0.86901834 -0.4617027 -0.08578219 0.3892144  0.23668737
4    4 -0.5555409 -0.02013537 -0.1503822 -0.14444023 0.7222297  0.36990686
 library(reshape)
set.seed(20)
gcz<-data.frame(subj=rep(1:4,rep(3,4)),time=rep(1:3),x=rnorm(12),y=rnorm(12))
gcz
   subj time           x           y
1     1    1  1.16268529 -0.62812676
2     1    2 -0.58592447  1.32322085
3     1    3  1.78546500 -1.52135057
4     2    1 -1.33259371 -0.43742787
5     2    2 -0.44656677  0.97057758
6     2    3  0.56960612  0.02822264
7     3    1 -2.88971761 -0.08578219
8     3    2 -0.86901834  0.38921440
9     3    3 -0.46170268  0.23668737
10    4    1 -0.55554091 -0.14444023
11    4    2 -0.02013537  0.72222970
12    4    3 -0.15038222  0.36990686
mgcz=melt(gcz,measure=c('x','y'))
mgcz
   subj time variable       value
1     1    1        x  1.16268529
2     1    2        x -0.58592447
3     1    3        x  1.78546500
4     2    1        x -1.33259371
5     2    2        x -0.44656677
6     2    3        x  0.56960612
7     3    1        x -2.88971761
8     3    2        x -0.86901834
9     3    3        x -0.46170268
10    4    1        x -0.55554091
11    4    2        x -0.02013537
12    4    3        x -0.15038222
13    1    1        y -0.62812676
14    1    2        y  1.32322085
15    1    3        y -1.52135057
16    2    1        y -0.43742787
17    2    2        y  0.97057758
18    2    3        y  0.02822264
19    3    1        y -0.08578219
20    3    2        y  0.38921440
21    3    3        y  0.23668737
22    4    1        y -0.14444023
23    4    2        y  0.72222970
24    4    3        y  0.36990686
cast(subj~variable+time,data=mgcz)
    subj        x_1            x_2                x_3               y_1             y_2             y_3
1    1  1.1626853 -0.58592447  1.7854650 -0.62812676 1.3232209 -1.52135057
2    2 -1.3325937 -0.44656677  0.5696061 -0.43742787 0.9705776  0.02822264
3    3 -2.8897176 -0.86901834 -0.4617027 -0.08578219 0.3892144  0.23668737
4    4 -0.5555409 -0.02013537 -0.1503822 -0.14444023 0.7222297  0.36990686
cast(subj~variable+time,subset=variable=='x',data=mgcz)
  subj        x_1         x_2        x_3
1    1  1.1626853 -0.58592447  1.7854650
2    2 -1.3325937 -0.44656677  0.5696061
3    3 -2.8897176 -0.86901834 -0.4617027
4    4 -0.5555409 -0.02013537 -0.1503822

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有