1. 首页
  2. 自学中心
  3. 软件
  4. R

R—给数据变个形—reshape2包

来自 http://blog.sina.com.cn/s/blog_670445240102v2pr.html

 

library(reshape2);

arc_genus_2<-read.table((“arc_otu.csv”,header=TRUE,sep=”\t”);

arc_otu.csv 数据存放位置http://pan.baidu.com/s/1mgFofuK

 

nn<-4;

aa<-arc_genus_2[,c(2:(nn+1),(nn+7))];

md<-melt(aa,id=”genus”);

ab<-dcast(md,genus~variable,sum);

md数据:

001SXUocgy6Mqgc21hI7eamp69

ab数据:

001SXUocgy6Mqgc5BbQ8aamp69

reshape2meltandcast

melt(data, id.vars,measure.vars,variable.name = “variable”, …, na.rm = FALSE,value.name = “value”)

dcast(data, formula, fun.aggregate = NULL, …, margins = NULL,

subset = NULL, fill = NULL, drop = TRUE,

value.var = guess_value(data))

acast(data, formula, fun.aggregate = NULL, …, margins = NULL,

subset = NULL, fill = NULL, drop = TRUE,

value.var = guess_value(data))

参考资料:

R语言实战

 

有兴趣的同学也可以看看陈丽云的博文:http://www.loyhome.com/探索R包reshape2:揉数据的最佳伴侣/

 

原创文章,作者:xsmile,如若转载,请注明出处:http://www.17bigdata.com/r-%e7%bb%99%e6%95%b0%e6%8d%ae%e5%8f%98%e4%b8%aa%e5%bd%a2-reshape2%e5%8c%85/

联系我们

在线咨询:点击这里给我发消息

邮件:23683716@qq.com

跳至工具栏