How to convert a time series object to a vector in R?

R ProgrammingServer Side ProgrammingProgramming

To convert a time series object into a vector, we just need to read that object with as.numeric and store it in some other object or in the same object. For example if we have a time series object x then it can be converted to a vector by using x<-as.numeric(x1). To check the difference between a time series object and a vector in R, we must print them.

Example

 Live Demo

x1<-ts(rnorm(10,2,0.12))
x1

Output

Time Series:
Start = 1
End = 10
Frequency = 1
[1] 1.951703 1.944470 2.126346 1.945275 2.129368 2.100713 1.992308 2.058306
[9] 2.271858 2.055763

Example

x1<-as.numeric(x1)
x1

Output

[1] 1.951703 1.944470 2.126346 1.945275 2.129368 2.100713 1.992308 2.058306
[9] 2.271858 2.055763

Example

is.vector(x1)

Output

[1] TRUE

Example

 Live Demo

x2<-ts(rnorm(80,25,2.25))
x2

Output

Time Series:
Start = 1
End = 80
Frequency = 1
[1] 24.45105 23.92942 23.02201 22.23093 24.27239 27.95645 26.34687 26.15606
[9] 24.06356 23.78549 25.12992 27.76245 23.10760 21.62202 21.83581 20.67305
[17] 24.59828 22.31877 24.12288 20.08288 27.02836 23.56667 23.34890 28.04439
[25] 21.32559 25.52859 22.82904 25.08876 27.68131 24.25492 23.52403 21.76799
[33] 24.30723 29.73283 28.21973 22.56300 21.80799 27.93293 27.13120 24.44693
[41] 26.88498 22.12067 26.01575 25.21267 28.24337 25.27242 26.30312 26.04764
[49] 24.32843 21.43456 23.17547 29.02772 26.94025 26.59245 23.83956 21.36977
[57] 28.94171 25.04432 24.26455 30.04485 24.42073 17.29558 29.60838 27.14759
[65] 23.25170 24.17799 26.63077 27.22228 28.94655 27.37721 23.64524 27.57805
[73] 24.35631 27.12630 23.30073 23.07521 24.33536 24.05408 24.72586 27.93678

Example

x2<-as.numeric(x2)
x2

Output

[1] 24.45105 23.92942 23.02201 22.23093 24.27239 27.95645 26.34687 26.15606
[9] 24.06356 23.78549 25.12992 27.76245 23.10760 21.62202 21.83581 20.67305
[17] 24.59828 22.31877 24.12288 20.08288 27.02836 23.56667 23.34890 28.04439
[25] 21.32559 25.52859 22.82904 25.08876 27.68131 24.25492 23.52403 21.76799
[33] 24.30723 29.73283 28.21973 22.56300 21.80799 27.93293 27.13120 24.44693
[41] 26.88498 22.12067 26.01575 25.21267 28.24337 25.27242 26.30312 26.04764
[49] 24.32843 21.43456 23.17547 29.02772 26.94025 26.59245 23.83956 21.36977
[57] 28.94171 25.04432 24.26455 30.04485 24.42073 17.29558 29.60838 27.14759
[65] 23.25170 24.17799 26.63077 27.22228 28.94655 27.37721 23.64524 27.57805
[73] 24.35631 27.12630 23.30073 23.07521 24.33536 24.05408 24.72586 27.93678

Example

 Live Demo

x3<-ts(rpois(200,5))
x3

Output

Time Series:
Start = 1
End = 200
Frequency = 1
[1] 5 3 4 7 5 3 8 3 2 5 3 6 7 3 5 3 5 7 3 7 6 3 4 2 7
[26] 4 5 4 7 1 5 4 9 9 4 6 3 4 2 1 4 4 3 7 3 3 3 4 4 3
[51] 5 5 6 8 6 9 3 4 2 6 5 3 4 4 3 5 5 5 8 3 0 8 1 6 5
[76] 5 5 1 6 6 8 5 6 8 3 6 5 3 3 13 6 8 8 4 7 6 5 3 6 4
[101] 6 4 3 3 7 3 3 6 5 4 5 5 3 11 4 3 5 7 3 10 4 9 1 4 4
[126] 6 7 2 7 4 4 5 4 8 4 2 5 6 4 8 4 6 5 2 3 7 12 3 6 2
[151] 4 5 6 4 4 3 5 4 8 4 6 9 7 5 4 5 7 2 6 5 6 6 10 6 4
[176] 7 4 9 7 5 3 4 2 4 5 5 4 5 3 8 5 4 4 3 6 7 5 2 6 6

Example

x3<-as.numeric(x3)
x3

Output

[1] 5 3 4 7 5 3 8 3 2 5 3 6 7 3 5 3 5 7 3 7 6 3 4 2 7
[26] 4 5 4 7 1 5 4 9 9 4 6 3 4 2 1 4 4 3 7 3 3 3 4 4 3
[51] 5 5 6 8 6 9 3 4 2 6 5 3 4 4 3 5 5 5 8 3 0 8 1 6 5
[76] 5 5 1 6 6 8 5 6 8 3 6 5 3 3 13 6 8 8 4 7 6 5 3 6 4
[101] 6 4 3 3 7 3 3 6 5 4 5 5 3 11 4 3 5 7 3 10 4 9 1 4 4
[126] 6 7 2 7 4 4 5 4 8 4 2 5 6 4 8 4 6 5 2 3 7 12 3 6 2
[151] 4 5 6 4 4 3 5 4 8 4 6 9 7 5 4 5 7 2 6 5 6 6 10 6 4
[176] 7 4 9 7 5 3 4 2 4 5 5 4 5 3 8 5 4 4 3 6 7 5 2 6 6

Example

 Live Demo

x4<-ts(runif(60,2,10))
x4

Output

Time Series:
Start = 1
End = 60
Frequency = 1
[1] 3.345878 6.077496 2.116746 2.732752 6.423906 7.511018 5.981971 5.751605
[9] 4.371582 6.799967 2.978181 2.623707 7.704259 2.744488 3.328368 4.809371
[17] 6.638952 4.577783 5.092903 9.020576 2.068590 3.928907 5.158880 5.166288
[25] 4.848047 4.250751 5.821842 5.724591 2.179566 7.717782 7.239084 8.383315
[33] 8.766395 6.540062 7.962879 6.506263 9.687081 4.303368 9.670949 3.698642
[41] 6.218613 6.798307 6.594818 5.314983 2.218027 6.182458 8.672256 6.713384
[49] 7.284777 7.547708 7.320426 2.682290 4.702898 8.570250 2.685032 4.352063
[57] 4.285486 8.574128 7.032187 4.189285

Example

x4<-as.numeric(x4)
x4

Output

[1] 3.345878 6.077496 2.116746 2.732752 6.423906 7.511018 5.981971 5.751605
[9] 4.371582 6.799967 2.978181 2.623707 7.704259 2.744488 3.328368 4.809371
[17] 6.638952 4.577783 5.092903 9.020576 2.068590 3.928907 5.158880 5.166288
[25] 4.848047 4.250751 5.821842 5.724591 2.179566 7.717782 7.239084 8.383315
[33] 8.766395 6.540062 7.962879 6.506263 9.687081 4.303368 9.670949 3.698642
[41] 6.218613 6.798307 6.594818 5.314983 2.218027 6.182458 8.672256 6.713384
[49] 7.284777 7.547708 7.320426 2.682290 4.702898 8.570250 2.685032 4.352063
[57] 4.285486 8.574128 7.032187 4.189285
raja
Published on 07-Dec-2020 06:06:06
Advertisements