R 如何重塑下表?
这里有几个类似的问题,但我没有将它们应用到我的案例中。我有很多这样的桌子:R 如何重塑下表?,r,R,这里有几个类似的问题,但我没有将它们应用到我的案例中。我有很多这样的桌子: GDP: time | AT | BE | BG | CF | : United kingdom 2014Q4 | 4564 | 4646 | 6541 | 122 | : 2014Q3 | 1234 | 5789 | 3545 | 3546 | : 2014Q2 | 1456 | 35
GDP
:
time | AT | BE | BG | CF | : United kingdom
2014Q4 | 4564 | 4646 | 6541 | 122 | :
2014Q3 | 1234 | 5789 | 3545 | 3546 | :
2014Q2 | 1456 | 354 | 3541 | 3543 | :
: | : | : | : | : | :
1990Q1 | 1234 | 3546 | 6546 | 5466 |
实际有效汇率:
类似的表格等
如何将其重塑为以下样式:
Country | time | GDP | REER | :..
AT | 1990Q1 | 4564 | 4646 | 6541 | 122 | :
AT | 1990Q2 | 1234 | 5789 | 3545 | 3546 | :
AT | 1990Q3 | 1456 | 354 | 3541 | 3543 |
: | : | : | : | : | : | :
United Kingdom | 2014Q4 | 1234 | 3546 | 6546 | 5466 |
代码
setwd ("D:/Documents/R/eurostatData")
library (SmarterPoland)
library(reshape)
library (xlsx)
# Write to xls file --------------------------------------------------------
save.xlsx <- function (file, ...)
{
require(xlsx, quietly = TRUE)
objects <- list(...)
fargs <- as.list(match.call(expand.dots = TRUE))
objnames <- as.character(fargs)[-c(1, 2)]
nobjects <- length(objects)
for (i in 1:nobjects) {
if (i == 1)
write.xlsx(objects[[i]], file, sheetName = objnames[i])
else write.xlsx(objects[[i]], file, sheetName = objnames[i],
append = TRUE)
}
print(paste("Workbook", file, "has", nobjects, "worksheets."))
}
# load TOC
TOC <- grepEurostatTOC("Production") # get everything about Production
TOC2 <- getEurostatTOC()
View(TOC)
# END TOC
dat_raw <- getEurostatRCV("sts_inpr_q") # quaterly
# how many unique values are?
unique(dat_raw$indic_bt)
unique(dat_raw$nace_r2) # initialy I need C - manufacturing
unique(dat_raw$time) # 1980-20104
production <- cast(dat_raw, time ~ geo, subset= nace_r2=="C" & s_adj=="GROSS" )
# RESHAPING
# !!!!nothing to write, since the steps I did were not successful.!!!!
save.xlsx("data.xlsx", production, turnover_dom, turnover_ndom, import_price, producer_prices, labour, GCF, gdp, inflation)
setwd(“D:/Documents/R/eurostatData”)
图书馆(智能乐园)
图书馆(重塑)
图书馆(xlsx)
#写入xls文件--------------------------------------------------------
save.xlsx您可以使用tidyr包中的gather
GDP <- gather(GDP_table, Country, GDP, -time)
REER <- gather(REER_table, Country, REER, -time)
GDP您可以从提供一个数据结构开始,这样我们就不必猜测您的数据结构到底是什么样的!它不必是真实的数据,只需要足够的数据就可以创建您试图解决的问题。我认为您正在尝试转置:t()
我猜您可能需要类似于rbind
和重塑
(或melt
,但它必须是“data.table”开发版本中的melt
,因为您需要半宽数据集)。我正在玩重塑和熔化,但没有结果expected@Roman,请使用“编辑”按钮为您的问题添加更多详细信息。注释只允许非常有限的格式设置,因此对共享代码不是很有用。谢谢您的建议!!
data <- merge(GDP, REER)