如何在 R 中将数据框转换为时间序列对象后,提取包含名称的列?


在 R 中访问数据框的列,我们只需要使用 $ 符号,但如果数据框被转换为时间序列对象,则所有列都将表现为时间序列,因此,我们不能简单地使用 $ 符号。为此,我们需要使用单方括号并将其中的适当列传递进去。请查看以下示例以了解其工作原理。

示例 1

考虑以下数据框

在线演示

> set.seed(147)
> x1<-rpois(20,5)
> x2<-rpois(20,8)
> x3<-rpois(20,3)
> df1<-data.frame(x1,x2,x3)
> df1

输出

x1 x2 x3
1 5 11 4
2 5 5 3
3 4 6 2
4 10 8 1
5 4 6 3
6 4 9 3
7 9 7 4
8 4 4 1
9 3 8 6
10 5 9 2
11 4 13 2
12 4 6 3
13 6 5 2
14 8 10 3
15 1 10 3
16 5 9 3
17 7 8 6
18 7 5 0
19 4 8 2
20 5 5 7

将 df1 转换为时间序列对象

示例

> df1_time_series<-ts(df1)
> df1_time_series
Time Series:
Start = 1
End = 20
Frequency = 1

输出

x1 x2 x3
1 5 11 4
2 5 5 3
3 4 6 2
4 10 8 1
5 4 6 3
6 4 9 3
7 9 7 4
8 4 4 1
9 3 8 6
10 5 9 2
11 4 13 2
12 4 6 3
13 6 5 2
14 8 10 3
15 1 10 3
16 5 9 3
17 7 8 6
18 7 5 0
19 4 8 2
20 5 5 7

提取上述时间序列的列

示例

> df1_time_series[,"x1"]
Time Series:
Start = 1
End = 20
Frequency = 1

输出

[1] 5 5 4 10 4 4 9 4 3 5 4 4 6 8 1 5 7 7 4 5

示例

> df1_time_series[,"x2"]
Time Series:
Start = 1
End = 20
Frequency = 1

输出

[1] 11 5 6 8 6 9 7 4 8 9 13 6 5 10 10 9 8 5 8 5

示例

> df1_time_series[,"x3"]
Time Series:
Start = 1
End = 20
Frequency = 1

输出

[1] 4 3 2 1 3 3 4 1 6 2 2 3 2 3 3 3 6 0 2 7

示例 2

在线演示

> y1<-rnorm(20,1,0.25)
> y2<-rnorm(20,1,0.078)
> y3<-rnorm(20,1,0.045)
> y4<-rnorm(20,1,0.65)
> df2<-data.frame(y1,y2,y3,y4)
> df2

输出

       y1       y2        y3       y4
1 0.4610082 1.1123116 0.9937312 1.60152771
2 1.2245278 1.1441032 0.9955816 1.01301470
3 0.9281928 0.9471151 1.0130205 1.73380614
4 0.6132334 0.9914514 1.0478584 1.12878115
5 0.8047991 0.9364563 1.0559170 0.11453683
6 1.3873896 0.9890774 0.8793818 1.08303443
7 0.8734964 0.9923517 1.0456627 1.40754764
8 0.5829787 1.1520386 1.0679080 -0.06112731
9 0.7886331 1.2120417 1.0131238 1.12503045
10 1.4817215 1.1045179 0.9894544 1.00392323
11 1.1166086 0.9957914 0.9241877 0.37224585
12 1.0734553 1.0714675 1.0013594 0.46353553
13 1.0378841 0.9814108 1.0169206 1.57986107
14 0.5939274 0.9737219 1.0043724 0.17741973
15 1.1111737 0.9444893 1.0601156 0.96969383
16 1.2379935 0.9730605 1.0632339 0.39235006
17 1.2920541 0.8550713 0.9872660 0.42308594
18 0.7378359 1.0077608 1.0571702 1.34754960
19 0.7497949 0.9085073 1.0041391 1.04504683
20 1.0315004 1.1117264 0.9580732 1.13297488

示例

> df2_ts<-ts(df2)
> df2_ts
Time Series:
Start = 1
End = 20
Frequency = 1

输出

     y1          y2        y3       y4
1 0.4610082 1.1123116 0.9937312 1.60152771
2 1.2245278 1.1441032 0.9955816 1.01301470
3 0.9281928 0.9471151 1.0130205 1.73380614
4 0.6132334 0.9914514 1.0478584 1.12878115
5 0.8047991 0.9364563 1.0559170 0.11453683
6 1.3873896 0.9890774 0.8793818 1.08303443
7 0.8734964 0.9923517 1.0456627 1.40754764
8 0.5829787 1.1520386 1.0679080 -0.06112731
9 0.7886331 1.2120417 1.0131238 1.12503045
10 1.4817215 1.1045179 0.9894544 1.00392323
11 1.1166086 0.9957914 0.9241877 0.37224585
12 1.0734553 1.0714675 1.0013594 0.46353553
13 1.0378841 0.9814108 1.0169206 1.57986107
14 0.5939274 0.9737219 1.0043724 0.17741973
15 1.1111737 0.9444893 1.0601156 0.96969383
16 1.2379935 0.9730605 1.0632339 0.39235006
17 1.2920541 0.8550713 0.9872660 0.42308594
18 0.7378359 1.0077608 1.0571702 1.34754960
19 0.7497949 0.9085073 1.0041391 1.04504683
20 1.0315004 1.1117264 0.9580732 1.13297488

示例

> df2_ts[,"y1"]
Time Series:
Start = 1
End = 20
Frequency = 1

输出

[1]  0.4610082 1.2245278 0.9281928 0.6132334 0.8047991 1.3873896 0.8734964
[8]  0.5829787 0.7886331 1.4817215 1.1166086 1.0734553 1.0378841 0.5939274
[15] 1.1111737 1.2379935 1.2920541 0.7378359 0.7497949 1.0315004

示例

> df2_ts[,"y2"]
Time Series:
Start = 1
End = 20
Frequency = 1

输出

[1] 1.1123116 1.1441032 0.9471151 0.9914514 0.9364563 0.9890774 0.9923517
[8] 1.1520386 1.2120417 1.1045179 0.9957914 1.0714675 0.9814108 0.9737219
[15] 0.9444893 0.9730605 0.8550713 1.0077608 0.9085073 1.1117264

示例

> df2_ts[,"y3"]
Time Series:
Start = 1
End = 20
Frequency = 1

输出

[1] 0.9937312 0.9955816 1.0130205 1.0478584 1.0559170 0.8793818 1.0456627
[8] 1.0679080 1.0131238 0.9894544 0.9241877 1.0013594 1.0169206 1.0043724
[15] 1.0601156 1.0632339 0.9872660 1.0571702 1.0041391 0.9580732

示例

> df2_ts[,"y4"]
Time Series:
Start = 1
End = 20
Frequency = 1

输出

[1]  1.60152771 1.01301470 1.73380614 1.12878115 0.11453683 1.08303443
[7]  1.40754764 -0.06112731 1.12503045 1.00392323 0.37224585 0.46353553
[13] 1.57986107 0.17741973 0.96969383 0.39235006 0.42308594 1.34754960
[19] 1.04504683 1.13297488

更新于: 2020-11-23

378 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告