How to extract columns of a data frame with their names after converting it to a time series object in R?


To access columns of data frame in R, we just need to use $ sign but if the data frame is converted to a time series object then all the columns will behave as a time series, hence, we cannot simply use $ sign. For this purpose, we would need to use single square brackets and pass the appropriate column inside it. Look at the below examples to understand how it works.

Example 1

Consider the below data frame:

Live Demo

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

Output

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

Converting df1 to time series object:

Example

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

Output

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

Extracting columns of the above time series:

Example

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

Output

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

Example

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

Output

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

Example

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

Output

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

Example2

Live Demo

> 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

Output

       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

Example

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

Output

     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

Example

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

Output

[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

Example

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

Output

[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

Example

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

Output

[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

Example

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

Output

[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

Updated on: 23-Nov-2020

353 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements