【问题标题】:"Aggregating" non-numeric variables in Reshape在 Reshape 中“聚合”非数字变量
【发布时间】:2012-05-10 03:50:59
【问题描述】:

我有一个长格式数据集,并希望使用 Reshape 或 Reshape 之前的任何预处理将其转换为宽格式。困难在于“值”变量是非数字的。请注意,原始数据中也有合法的重复记录。以下代码显示了每个的数据布局。

id = c(1, 1, 1, 1, 1, 1, 1)
month <- c("jan", "feb", "feb", "march", "april", "april", "april")
stress <- c("mild", "mild", "high", "moderate", "mild", "high", "mild")
Longdata <- data.frame(id, month, stress, stringsAsFactors = FALSE)

这是原始格式:

> Longdata
  id month   stress
1  1   jan     mild
2  1   feb     mild
3  1   feb     high
4  1 march moderate
5  1 april     mild
6  1 april     high
7  1 april     mild

这就是我想要组织数据的方式:

id <- c(1)
jan <- c("mild")
feb <- c("mild-high")
march <- c("moderate")
april <- c("mild-high-mild")
widedata <- data.frame(id, jan, feb, march, april, stringsAsFactors = FALSE)
> widedata
  id  jan       feb    march          april
1  1 mild mild-high moderate mild-high-mild

【问题讨论】:

    标签: r aggregate reshape


    【解决方案1】:

    您可以分两步执行此操作,首先使用 aggregate,然后使用“reshape2”包中的基本 R reshapedcast

    1. 聚合步骤:

      Mediumdata <- aggregate(stress ~ id + month, Longdata, paste, collapse="-")
      Mediumdata
      #   id month         stress
      # 1  1 april mild-high-mild
      # 2  1   feb      mild-high
      # 3  1   jan           mild
      # 4  1 march       moderate
      
    2. 重塑步骤:

      # Using base R reshape
      reshape(Mediumdata, direction="wide", idvar="id", timevar="month")
      #   id   stress.april stress.feb stress.jan stress.march
      # 1  1 mild-high-mild  mild-high       mild     moderate
      
      # Using `dcast` from "reshape2"
      dcast(mediumdata, id ~ month, value.var="stress")
      #   id          april       feb  jan    march
      # 1  1 mild-high-mild mild-high mild moderate
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      相关资源
      最近更新 更多