我得到了包含多个国家的多个宏观经济变量的17个数据框的列表,数据框的结构如下:
df$CPI
Date US Argentina Vietnam India Indonesia Philippines
1564531200 1.8 54.4 2.4 3.1 3.3 2.4
1561852800 1.6 55.8 2.2 3.2 3.3 2.7
1559260800 1.8 57.3 2.9 3.0 3.3 3.2
df$CapitalAccount
Date US Argentina Brazil China Turkey Thai
2019-06-30 0 13.8 49.0 -58.5 -7.2 27.9
2019-03-31 0 32.2 98.1 -26.3 21.4 0.0
2018-12-31 2721 16.2 59.8 -213.1 0.5 0.0
2018-06-30 -5 10.9 82.0 -50.6 -2.7 0.0
我正在尝试按国家/地区名称重新组织这些数据框,例如:
US
Date CPI CapitalAccount .......(the other 14 macro variables)
2019-06-30
2019-03-31
2018-12-31
Argentina
Date CPI CapitalAccount .......(the other 14 macro variables)
2019-06-30
2019-03-31
2018-12-31
.
.
.
.
我尝试使用for循环遍历数据帧列表中的每个数据帧,并按该数据帧的colnames()抓住该列,但是它不起作用,结果给了我很多重复的NA和日期。
For US:
for (i in 1:length(df)){
NewUS <- df[[i]][,which(colnames(df[[i]])=='US')]
US <- merge(US, NewUS)
i <- i+1
}
US
For Argentina:
for (i in 1:length(df)){
NewArgentina <- df[[i]][,which(colnames(df[[i]])=='Argentina')]
Argentina <- merge(Argentina, NewArgentina)
i <- i+1
}
Argentina
答案 0 :(得分:2)
编辑:根据@Gregor的建议。我用idcol
和fill = T
替换了for循环。
希望这可以帮助。在下面的代码中,df1
和df2
是伪数据表。在您的情况下,它们将是CPI,CapitalAccount ...
首先,我们从每个表中选择列,在名为type
的列表中的每个数据表中添加一个新列,并在该列中分配经济变量。接下来,既然您的数据表具有确切的列,我们将使用rbindlist()
来绑定列表。
library(data.table)
df1 <- data.table(date = rep(seq(from = as.Date('2019-01-01'), to = as.Date('2019-01-05'), by = 'day'), 5),
US = runif(25),
Argentina = runif(25),
Thailand = runif(25),
China = runif(25))
df2 <- data.table(date = rep(seq(from = as.Date('2019-01-01'), to = as.Date('2019-01-05'), by = 'day'), 5),
US = runif(25),
Argentina = runif(25),
Japan = runif(25))
l1 <- list(df1, df2)
names(l1) <- c('GDP', 'CPI')
x <- rbindlist(l1, idcol = 'type', fill = TRUE) # this works even when the columns are different for each table
现在我们将所有数据表组合在一起,我们可以调整表的形状以使其看起来像您想要的结果。
x1 <- melt(x, id.vars = c('date', 'type'), measure.vars = c('US', 'Argentina'), variable.name = 'country', value.name = 'value')
dcast(x1, date + country ~ type, value.var = 'value')
答案 1 :(得分:0)
考虑具有reshape
,链merge
和split
的基数R,以获取转换后的数据帧命名列表。辅助函数包括高阶Map
和Reduce
。
proc_reshape <- function(df, nm) {
within(data.frame(reshape(df, varying = names(df)[-1], times = names(df)[-1],
v.names = nm, timevar = "Country", direction = "long"),
row.names = NULL), {
Date <- as.Date(as.POSIXct(Date, origin = "1970-01-01"))
rm(id)
})
}
# ELEMENTWISE LOOP THROUGH DFs AND THEIR NAMES
long_list <- Map(proc_reshape, my_list, names(my_list))
# CHAIN MERGE (FULL JOIN FOR MISMATCHED DATES BY COUNTRY)
merged_df <- Reduce(function(x, y) merge(x, y, by = c("Country", "Date"), all = TRUE),
long_list)
# CREATE NEW NAMED LIST OF DFs
new_list <- split(merged_df, merged_df$Country)
输出
new_list
$Argentina
Country Date CPI CapitalAccount
1 Argentina 2018-06-29 NA 10.9
2 Argentina 2018-12-30 NA 16.2
3 Argentina 2019-03-30 NA 32.2
4 Argentina 2019-05-31 57.3 NA
5 Argentina 2019-06-29 NA 13.8
6 Argentina 2019-06-30 55.8 NA
7 Argentina 2019-07-31 54.4 NA
$Brazil
Country Date CPI CapitalAccount
8 Brazil 2018-06-29 NA 82.0
9 Brazil 2018-12-30 NA 59.8
10 Brazil 2019-03-30 NA 98.1
11 Brazil 2019-06-29 NA 49.0
$China
Country Date CPI CapitalAccount
12 China 2018-06-29 NA -50.6
13 China 2018-12-30 NA -213.1
14 China 2019-03-30 NA -26.3
15 China 2019-06-29 NA -58.5
$India
Country Date CPI CapitalAccount
16 India 2019-05-31 3.0 NA
17 India 2019-06-30 3.2 NA
18 India 2019-07-31 3.1 NA
$Indonesia
Country Date CPI CapitalAccount
19 Indonesia 2019-05-31 3.3 NA
20 Indonesia 2019-06-30 3.3 NA
21 Indonesia 2019-07-31 3.3 NA
$Philippines
Country Date CPI CapitalAccount
22 Philippines 2019-05-31 3.2 NA
23 Philippines 2019-06-30 2.7 NA
24 Philippines 2019-07-31 2.4 NA
$Thai
Country Date CPI CapitalAccount
25 Thai 2018-06-29 NA 0.0
26 Thai 2018-12-30 NA 0.0
27 Thai 2019-03-30 NA 0.0
28 Thai 2019-06-29 NA 27.9
$Turkey
Country Date CPI CapitalAccount
29 Turkey 2018-06-29 NA -2.7
30 Turkey 2018-12-30 NA 0.5
31 Turkey 2019-03-30 NA 21.4
32 Turkey 2019-06-29 NA -7.2
$US
Country Date CPI CapitalAccount
33 US 2018-06-29 NA -5
34 US 2018-12-30 NA 2721
35 US 2019-03-30 NA 0
36 US 2019-05-31 1.8 NA
37 US 2019-06-29 NA 0
38 US 2019-06-30 1.6 NA
39 US 2019-07-31 1.8 NA
$Vietnam
Country Date CPI CapitalAccount
40 Vietnam 2019-05-31 2.9 NA
41 Vietnam 2019-06-30 2.2 NA
42 Vietnam 2019-07-31 2.4 NA