将时间序列数据重塑为 r 中具有多个主题和变量的面板数据
Reshape time-series data into panel-data with multiple subjects and variables in r
我有 raw panel data in the following format and would like to reshape it into the classic panel data 格式,然后在面板数据回归中使用它。
以数字开头的列名是 excel 日期格式的时间。重塑数据后,应该有公司和时间列以及 "Variable" 列中列出的数据项的列。
来自 R:
dput(head(df.example))
structure(list(Firm = c("ABB LTD N", "Facebook", "Nestle", "ABB LTD N",
"Facebook", "Nestle"), Variable = c("Price", "Price", "Price",
"Market Value", "Market Value", "Market Value"), `32508` = c(110.67,
162500, 14.355, 809735, 9.647, 2223.87), `32538` = c(110.35,
162000, 14.355, 809735, 8.836, 2036.94), `32568` = c(115.29,
16925, 14.355, 809735, 10.556, 2433.36), `32598` = c(130.61,
19175, 14.355, 809735, 11.744, 2707.32), `32628` = c(146.34,
4130, 14.355, 809735, 12.975, 162000)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))
我尝试用 reshape2::melt 和 reshape2::cast 函数来做,但没有用。我找不到类似的问题。非常感谢。
我不确定你的列名是否带有数字,但解决方法是使用 data.table
。 (考虑到 Excel 来源 ("1900-01-01")
,我将离开日期,如 ?as.Date
中所述)
library(data.table)
df2 <- melt(df,id = c("Firm","Variable"), variable.name = "date")
setDT(df2)[,date := as.Date(as.numeric(paste(date)), origin = "1900-01-01"))]
df2
#returns
Firm Variable date value
1: ABB LTD N Price 1989-01-02 110.670
2: Facebook Price 1989-01-02 162500.000
3: Nestle Price 1989-01-02 14.355
4: ABB LTD N Market Value 1989-01-02 809735.000
5: Facebook Market Value 1989-01-02 9.647
6: Nestle Market Value 1989-01-02 2223.870
7: ABB LTD N Price 1989-02-01 110.350
8: Facebook Price 1989-02-01 162000.000
9: Nestle Price 1989-02-01 14.355
10: ABB LTD N Market Value 1989-02-01 809735.000
11: Facebook Market Value 1989-02-01 8.836
12: Nestle Market Value 1989-02-01 2036.940
13: ABB LTD N Price 1989-03-03 115.290
14: Facebook Price 1989-03-03 16925.000
15: Nestle Price 1989-03-03 14.355
16: ABB LTD N Market Value 1989-03-03 809735.000
17: Facebook Market Value 1989-03-03 10.556
18: Nestle Market Value 1989-03-03 2433.360
19: ABB LTD N Price 1989-04-02 130.610
20: Facebook Price 1989-04-02 19175.000
21: Nestle Price 1989-04-02 14.355
22: ABB LTD N Market Value 1989-04-02 809735.000
23: Facebook Market Value 1989-04-02 11.744
24: Nestle Market Value 1989-04-02 2707.320
25: ABB LTD N Price 1989-05-02 146.340
26: Facebook Price 1989-05-02 4130.000
27: Nestle Price 1989-05-02 14.355
28: ABB LTD N Market Value 1989-05-02 809735.000
29: Facebook Market Value 1989-05-02 12.975
30: Nestle Market Value 1989-05-02 162000.000
Firm Variable date value
我有 raw panel data in the following format and would like to reshape it into the classic panel data 格式,然后在面板数据回归中使用它。
以数字开头的列名是 excel 日期格式的时间。重塑数据后,应该有公司和时间列以及 "Variable" 列中列出的数据项的列。
来自 R:
dput(head(df.example))
structure(list(Firm = c("ABB LTD N", "Facebook", "Nestle", "ABB LTD N",
"Facebook", "Nestle"), Variable = c("Price", "Price", "Price",
"Market Value", "Market Value", "Market Value"), `32508` = c(110.67,
162500, 14.355, 809735, 9.647, 2223.87), `32538` = c(110.35,
162000, 14.355, 809735, 8.836, 2036.94), `32568` = c(115.29,
16925, 14.355, 809735, 10.556, 2433.36), `32598` = c(130.61,
19175, 14.355, 809735, 11.744, 2707.32), `32628` = c(146.34,
4130, 14.355, 809735, 12.975, 162000)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))
我尝试用 reshape2::melt 和 reshape2::cast 函数来做,但没有用。我找不到类似的问题。非常感谢。
我不确定你的列名是否带有数字,但解决方法是使用 data.table
。 (考虑到 Excel 来源 ("1900-01-01")
,我将离开日期,如 ?as.Date
中所述)
library(data.table)
df2 <- melt(df,id = c("Firm","Variable"), variable.name = "date")
setDT(df2)[,date := as.Date(as.numeric(paste(date)), origin = "1900-01-01"))]
df2
#returns
Firm Variable date value
1: ABB LTD N Price 1989-01-02 110.670
2: Facebook Price 1989-01-02 162500.000
3: Nestle Price 1989-01-02 14.355
4: ABB LTD N Market Value 1989-01-02 809735.000
5: Facebook Market Value 1989-01-02 9.647
6: Nestle Market Value 1989-01-02 2223.870
7: ABB LTD N Price 1989-02-01 110.350
8: Facebook Price 1989-02-01 162000.000
9: Nestle Price 1989-02-01 14.355
10: ABB LTD N Market Value 1989-02-01 809735.000
11: Facebook Market Value 1989-02-01 8.836
12: Nestle Market Value 1989-02-01 2036.940
13: ABB LTD N Price 1989-03-03 115.290
14: Facebook Price 1989-03-03 16925.000
15: Nestle Price 1989-03-03 14.355
16: ABB LTD N Market Value 1989-03-03 809735.000
17: Facebook Market Value 1989-03-03 10.556
18: Nestle Market Value 1989-03-03 2433.360
19: ABB LTD N Price 1989-04-02 130.610
20: Facebook Price 1989-04-02 19175.000
21: Nestle Price 1989-04-02 14.355
22: ABB LTD N Market Value 1989-04-02 809735.000
23: Facebook Market Value 1989-04-02 11.744
24: Nestle Market Value 1989-04-02 2707.320
25: ABB LTD N Price 1989-05-02 146.340
26: Facebook Price 1989-05-02 4130.000
27: Nestle Price 1989-05-02 14.355
28: ABB LTD N Market Value 1989-05-02 809735.000
29: Facebook Market Value 1989-05-02 12.975
30: Nestle Market Value 1989-05-02 162000.000
Firm Variable date value