zoukankan      html  css  js  c++  java
  • 1、R-reshape2-cast

    1、cast:     长型数据转宽型数据

    (1)、acast,dcast的区别在于输出结果。acast 输出结果为vector/matrix/array,dcast 输出结果为data.frame。

    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))

    参数:

    data  要进行转换的数据框

    formula  用于转换的公式

    fun.aggregate   聚合函数,表达式为:行变量~列变量~三维变量~......,另外,.表示后面没有数据列,…表示之前或之后的所有数据列

    margins  用于添加边界汇总数据

    subset   用于添加过滤条件,需要载入plyr包

    其他三个参数,用到的情况相对较少。

    (2)、cast的例子

    x<-data.frame(id=1:6,

                  name=c("wang","zhang","li","chen","zhao","song"),
                  shuxue=c(89,85,68,79,96,53),
                  yuwen=c(77,68,86,87,92,63))
    library(reshape2)
    x1<-melt(x,id.vars=c("id","name"))
    acast(x1,id~variable)
    dcast(x1,id~variable)
    #从以上两个执行结果来看,可以看出acast和dcast的区别,这里acast输出结果省略了id这个列,而dcast则输出id列

    dcast(x1,id~variable,mean,margins=T)           #边缘多了对行、列求平均的结果

     dcast(x1,id~variable,mean,margins=c("id"))       #只求列平均值,当然也可以只对行求平均值,把id改成variable就可以了

    dcast(x1,id+name~variable)               #数据还原成原来的样子。

    dcast(x1,variable~name)                #对行列进行对调

  • 相关阅读:
    Max History CodeForces
    Buy a Ticket CodeForces
    AC日记——字符串的展开 openjudge 1.7 35
    AC日记——回文子串 openjudge 1.7 34
    AC日记——判断字符串是否为回文 openjudge 1.7 33
    AC日记——行程长度编码 openjudge 1.7 32
    AC日记——字符串P型编码 openjudge 1.7 31
    AC日记——字符环 openjudge 1.7 30
    AC日记——ISBN号码 openjudge 1.7 29
    AC日记——单词倒排 1.7 28
  • 原文地址:https://www.cnblogs.com/renping/p/6885899.html
Copyright © 2011-2022 走看看