• 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/

  • 相关阅读:
    『Delphi』字符串操作——返回子串出现的位置
    2007:远见、劲取、专注
    『转载』个人博客吸引风投关注成可盈利业务
    [和管子对话] 1 200745/对面向对象的你言我语
    『Delphi』File not found的解决办法
    Ruby学习1字符串
    聚集表(clustered table)data page中的数据行可以无序
    通过DBCC PAGE查看页信息验证聚集索引和非聚集索引节点信息
    查看SQL Server Resource Database以及修改系统表
    SQL Server的还原(2)——STOPAT
  • 原文地址:https://www.cnblogs.com/emanlee/p/9006167.html
Copyright © 2020-2023  润新知