【问题标题】:Creating longitudinal datasets with reshape使用重塑创建纵向数据集
【发布时间】:2012-12-16 10:12:09
【问题描述】:

我有数据集:

top100_repository_name  month   monthly_increase    monthly_begin_at    monthly_end_with
Bukkit                  2012-03 9                   431                 440
Bukkit                  2012-04 19                  438                 457
Bukkit                  2012-05 19                  455                 474
CodeIgniter             2012-03 15                  492                 507
CodeIgniter             2012-04 50                  506                 556
CodeIgniter             2012-05 19                  555                 574

我使用以下 R 代码:

library(reshape)
latent.growth.data <- read.csv(file = "LGC_data.csv", header = TRUE)
melt(latent.growth.data, id = c("top100_repository_name", "month"), measured = c("monthly_end_with"))
cast(latent.growth.data, top100_repository_name + month ~ monthly_end_with)

我想用它来创建具有以下结构的数据集:

top100_repository_name    2012-03    2012-04    2012-05
Bukkit                    440        457        474
CodeIgniter               507        556        574

但是,当我运行我的代码时,我得到以下输出:

Using monthly_end_with as value column.  Use the value argument to cast to override this choice
Error in `[.data.frame`(data, , variables, drop = FALSE) : 
  undefined columns selected

如何修改我的代码以生成所需的输出?

【问题讨论】:

  • 我很确定我的编辑是正确的,但请验证。
  • 您需要做几件事:(i) 将融化的结果保存到一个对象中,比如latent.growth.melt,然后按照下面的在latent.growth.melt 上运行演员表。如果您使用更新的 reshape2 包(推荐),则使用 dcast() 而不是 cast() - 最后一行应该类似于 dcast(latent.growth.melt, top100_repository_name ~ month, value.var = "value")。您可以通过查看 latent.growth.melt 来了解原因。

标签: r reshape data-manipulation


【解决方案1】:

我敢肯定,很快就会有人提出plyr 解决方案,但这里是使用reshape 函数的基本解决方案。

test <- read.table(textConnection("top100_repository_name  month   monthly_increase    monthly_begin_at    monthly_end_with
Bukkit                  2012-03 9                   431                 440
Bukkit                  2012-04 19                  438                 457
Bukkit                  2012-05 19                  455                 474
CodeIgniter             2012-03 15                  492                 507
CodeIgniter             2012-04 50                  506                 556
CodeIgniter             2012-05 19                  555                 574"),header=TRUE)

重塑这里的数据:

test2 <- reshape(
    test[c("top100_repository_name","month","monthly_end_with")],
    idvar="top100_repository_name",
    timevar="month",
    direction="wide"
)

修改名称

names(test2) <- gsub("monthly_end_with.","",names(test2))

看起来像:

> test2
  top100_repository_name 2012-03 2012-04 2012-05
1                 Bukkit     440     457     474
4            CodeIgniter     507     556     574

【讨论】:

  • +1,当其他人从“reshape2”跳到dcast时,我通常会尝试使用基本R方法,但这次你强迫我......;)
【解决方案2】:

这是基础 R 中另一种非常直接的方法。使用 xtabs()

xtabs(monthly_end_with ~ top100_repository_name + month, test)
#                       month
# top100_repository_name 2012-03 2012-04 2012-05
#            Bukkit          440     457     474
#            CodeIgniter     507     556     574

as.data.frame.matrix(
  xtabs(monthly_end_with ~ top100_repository_name + month, test))
#             2012-03 2012-04 2012-05
# Bukkit          440     457     474
# CodeIgniter     507     556     574

或者,如@thelatemail 所示,“reshape2”包中的dcast 可以按如下方式使用:

dcast(test, top100_repository_name ~ month, value.var="monthly_end_with")
#   top100_repository_name 2012-03 2012-04 2012-05
# 1                 Bukkit     440     457     474
# 2            CodeIgniter     507     556     574

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多