gpt4 book ai didi

r - 使用 reshape 创建纵向数据集

转载 作者:行者123 更新时间:2023-12-04 11:39:07 24 4
gpt4 key购买 nike

我有数据集:

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

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

最佳答案

我敢肯定,很快就会有人使用 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)

reshape 这里的数据:

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

关于r - 使用 reshape 创建纵向数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14115357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com