zoukankan      html  css  js  c++  java
  • reshape2

    require(reshape2)
    x = data.frame(subject = c("John", "Mary"),
                    time = c(1,1),
                    age = c(33,NA),
                    weight = c(90, NA),
                    height = c(2,2))
    x
      subject time age weight height
    1    John    1  33     90      2
    2    Mary    1  NA     NA      2


    molten = melt(x, id = c("subject", "time"))
    molten
      subject time variable value
    1    John    1      age    33
    2    Mary    1      age    NA
    3    John    1   weight    90
    4    Mary    1   weight    NA
    5    John    1   height     2
    6    Mary    1   height     2


    molten = melt(x, id = c("subject", "time"))
    molten
      subject time variable value
    1    John    1      age    33
    2    Mary    1      age    NA
    3    John    1   weight    90
    4    Mary    1   weight    NA
    5    John    1   height     2
    6    Mary    1   height     2


    molten = melt(x, id = c("subject", "time"), na.rm = TRUE)
    molten
      subject time variable value
    1    John    1      age    33
    3    John    1   weight    90
    5    John    1   height     2
    6    Mary    1   height     2


    dcast(molten, formula = time + subject ~ variable)
      time subject age weight height
    1    1    John  33     90      2
    2    1    Mary  NA     NA      2


    dcast(molten, formula = subject + time  ~ variable)
      subject time age weight height
    1    John    1  33     90      2
    2    Mary    1  NA     NA      2


    dcast(molten, formula = subject  ~ variable)
      subject age weight height
    1    John  33     90      2
    2    Mary  NA     NA      2


    dcast(molten, formula = ...  ~ variable)
      subject time age weight height
    1    John    1  33     90      2
    2    Mary    1  NA     NA      2

    REF:

    https://www.r-bloggers.com/reshape-and-aggregate-data-with-the-r-package-reshape2/

  • 相关阅读:
    python高级函数六剑客
    测试工程师用到常用的git命令
    qing理解赋值,深浅拷贝的区别
    python设计模式之单例
    Python全栈之jQuery笔记
    畅谈python之单元测试框架-unittest
    浅析python之单元测试框架-unittest
    Python之日志处理(logging模块)
    Spring整理
    Spark学习笔记11面向对象编程
  • 原文地址:https://www.cnblogs.com/emanlee/p/9006167.html
Copyright © 2011-2022 走看看