使用重塑创建纵向数据集

his*_*eim 2 r data-manipulation reshape

我有数据集:

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
Run Code Online (Sandbox Code Playgroud)

我使用以下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)
Run Code Online (Sandbox Code Playgroud)

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

top100_repository_name    2012-03    2012-04    2012-05
Bukkit                    440        457        474
CodeIgniter               507        556        574
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

the*_*ail 6

有人会很快找到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)
Run Code Online (Sandbox Code Playgroud)

在此重塑此数据:

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

修复名称

names(test2) <- gsub("monthly_end_with.","",names(test2))
Run Code Online (Sandbox Code Playgroud)

看起来像:

> test2
  top100_repository_name 2012-03 2012-04 2012-05
1                 Bukkit     440     457     474
4            CodeIgniter     507     556     574
Run Code Online (Sandbox Code Playgroud)