本文介绍了从长到宽聚合并重塑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过这个问题,但收到的回复与我的意愿不符。当时我用stata来做这项工作。但是,当我常规使用此类数据时,我希望使用R来创建所需的内容。我有按年龄,性别和诊断得出的每日住院数据。我希望从长到宽汇总并重塑数据。我怎样才能实现这个目标?示例数据和所需的输出如下所示。列标题指定性别,年龄和诊断的前缀。
谢谢

I have asked this question earlier and received a reply which was not in accordance with my wish. At the time I used stata to do the job. However as I routinely work with such data, I wish to use R to create what I wanted. I have a data set of daily hospital admission by age, sex and diagnoses. I wish to aggregate and reshape the data from long to wide. How could I achieve this objective? Sample data and required output are shown below. The column headers designate prefix of sex, age and diagnoses.Thanks

样本数据

structure(list(diag = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("card", "cere"), class = "factor"), sex = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Female", "Male"), class = "factor"),
    age = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("35-64",
    "65-74"), class = "factor"), admissions = c(1L, 1L, 0L, 0L,
    6L, 6L, 6L, 1L, 4L, 0L, 0L, 0L, 4L, 6L, 5L, 2L, 2L, 4L, 1L,
    0L, 6L, 5L, 6L, 4L), bdate = structure(c(1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
    3L, 3L, 3L, 3L, 3L), .Label = c("1987-01-01", "1987-01-02",
    "1987-01-03"), class = "factor")), .Names = c("diag", "sex",
"age", "admissions", "bdate"), row.names = c(NA, -24L), class = "data.frame")

必需的输出

structure(list(date = structure(1:3, .Label = c("01jan1987",
"02jan1987", "03jan1987"), class = "factor"), f3564card = c(1L,
4L, 2L), f6574card = c(1L, 0L, 4L), m3564card = c(0L, 0L, 1L),
    m6574card = c(0L, 0L, 0L), f3564cere = c(6L, 4L, 6L), f6574cere = c(6L,
    6L, 5L), m3564cere = c(6L, 5L, 6L), m6574cere = c(1L, 2L,
    4L)), .Names = c("date", "f3564card", "f6574card", "m3564card",
"m6574card", "f3564cere", "f6574cere", "m3564cere", "m6574cere"
), class = "data.frame", row.names = c(NA, -3L))


推荐答案

您的数据已经是长格式,可以通过 reshape2轻松使用,例如:

Your data are already in a long format that can be used easily by "reshape2", like this:

library(reshape)
dcast(df, bdate ~ sex + age + diag, value.var = "admissions")
#        bdate Female_35-64_card Female_35-64_cere Female_65-74_card Female_65-74_cere
# 1 1987-01-01                 1                 6                 1                 6
# 2 1987-01-02                 4                 4                 0                 6
# 3 1987-01-03                 2                 6                 4                 5
#   Male_35-64_card Male_35-64_cere Male_65-74_card Male_65-74_cere
# 1               0               6               0               1
# 2               0               5               0               2
# 3               1               6               0               4

我在示例输出中看不到任何汇总,但是如果需要汇总,您可以在 dcast 中使用 fun.aggregate 函数来实现。

I don't see any aggregation in your sample output, but if aggregation is required, you can achieve this with the fun.aggregate function within dcast.

这篇关于从长到宽聚合并重塑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 02:49