0
Year    A        B        C            D        E        F
1993-Q1 15.3    5.77    437.02      487.68     97       86.9
1993-Q2 13.5    5.74    455.2       504.5      94.7     85.4
1993-Q3 12.9    5.79    469.42      523.37     92.4     82.9
:::
2021-Q1 18.3    6.48    35680.82    29495.92    182.2   220.4
2021-Q2 7.9     6.46    36940.3     30562.03    180.4   218

Dataset1 <- read.csv('C:/Users/s/Desktop/R/intro/data/Dataset1.csv')

 class(Dataset1)
 [1] "data.frame"

time_series <- ts(Dataset1, start=1993, frequency = 4)

class(time_series)
[1] "mts"    "ts"     "matrix"

I don't know how to proceed from there to read my Year column as dates (quaterly) instead of numbers!

2 Answers2

1

Date class does not work well with ts class. It is better to use year and quarter. Using the input shown reproducibly in the Note at the end use read.csv.zoo with yearqtr class and then convert it to ts. The strip.white is probably not needed but we added it just in case.

library(zoo)

z <- read.csv.zoo("Dataset1.csv", FUN = as.yearqtr, format = "%Y-Q%q",
  strip.white = TRUE)
tt <- as.ts(z)

tt
##            A    B      C      D    E    F
## 1993 Q1 15.3 5.77 437.02 487.68 97.0 86.9
## 1993 Q2 13.5 5.74 455.20 504.50 94.7 85.4
## 1993 Q3 12.9 5.79 469.42 523.37 92.4 82.9

class(tt)
## [1] "mts"    "ts"     "matrix"

as.integer(time(tt)) # years
## [1] 1993 1993 1993

cycle(tt)  # quarters
##      Qtr1 Qtr2 Qtr3
## 1993    1    2    3

as.numeric(time(tt)) # time in years
## [1] 1993.00 1993.25 1993.50

If you did want to use Date class it would be better to use a zoo (or xts) series.

zd <- aggregate(z, as.Date, c)
zd
##               A    B      C      D    E    F
## 1993-01-01 15.3 5.77 437.02 487.68 97.0 86.9
## 1993-04-01 13.5 5.74 455.20 504.50 94.7 85.4
## 1993-07-01 12.9 5.79 469.42 523.37 92.4 82.9

If you want a data frame or xts object then fortify.zoo(z), fortify.zoo(zd), as.xts(z) or as.xts(zd) can be used depending on which one you want.

Note

Lines <- "Year,A,B,C,D,E,F
1993-Q1,15.3,5.77,437.02,487.68,97,86.9
1993-Q2,13.5,5.74,455.2,504.5,94.7,85.4
1993-Q3,12.9,5.79,469.42,523.37,92.4,82.9
"
cat(Lines, file = "Dataset1.csv")
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • i have another dilemma, i tried this code, and it says the figure margins too large, how do i fix that? plot(z$A, main = "A", xlab = "date", ylab = "gdp") – user17940819 Jan 15 '22 at 15:51
  • THANK YOU! i tried it and it works for my entire dataset. yes, i wanted it in xts form so this was helpful. – user17940819 Jan 15 '22 at 15:58
  • This code works after converting it to as.xts(zd) plot(zd$A, main = "A", xlab = "date", ylab = "gdp") – user17940819 Jan 15 '22 at 16:06
  • It works for both zoo and xts. You can also use `library(ggplot2); autoplot(z$A)` You can also pass just z in which case you will get a multi-panel plot. – G. Grothendieck Jan 15 '22 at 16:12
0

lubridate has really nice year-quarter function yq to convert year quarters to dates.

Dataset1<-structure(list(Year = c("1993-Q1", "1993-Q2", "1993-Q3", "1993-Q4", "1994-Q1", "1994-Q2"), ChinaGDP = c(15.3, 13.5, 12.9, 14.1, 14.1, 13.3), Yuan = c(5.77, 5.74, 5.79, 5.81, 8.72, 8.7), totalcredit = c(437.02, 455.2, 469.42, 521.68, 363.42, 389.01), bankcredit = c(487.68, 504.5, 523.37, 581.83, 403.48, 431.06), creditpercGDP = c(97, 94.7, 92.4, 95.6, 91.9, 90), creditGDPratio = c(86.9, 85.4, 82.9, 85.7, 82.8, 81.2)), row.names = c(NA, 6L), class = "data.frame")


library(lubridate)
library(dplyr)

df_quarter <- Dataset1 %>% 
  mutate(date=yq(Year)) %>%
  relocate(date, .after=Year)
  
df_quarter
#>      Year       date ChinaGDP Yuan totalcredit bankcredit creditpercGDP
#> 1 1993-Q1 1993-01-01     15.3 5.77      437.02     487.68          97.0
#> 2 1993-Q2 1993-04-01     13.5 5.74      455.20     504.50          94.7
#> 3 1993-Q3 1993-07-01     12.9 5.79      469.42     523.37          92.4
#> 4 1993-Q4 1993-10-01     14.1 5.81      521.68     581.83          95.6
#> 5 1994-Q1 1994-01-01     14.1 8.72      363.42     403.48          91.9
#> 6 1994-Q2 1994-04-01     13.3 8.70      389.01     431.06          90.0
#>   creditGDPratio
#> 1           86.9
#> 2           85.4
#> 3           82.9
#> 4           85.7
#> 5           82.8
#> 6           81.2

Created on 2022-01-15 by the reprex package (v2.0.1)

Joe Erinjeri
  • 1,200
  • 1
  • 7
  • 15
  • I am getting an error message when i tried this code, df_quarter <- df %>% mutate(date=yq(Year)) Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "function" – user17940819 Jan 15 '22 at 14:28
  • sorry you need ```dplyr``` for the pipes and mutate function. see edits above – Joe Erinjeri Jan 15 '22 at 14:30
  • I am still getting the same error :(( – user17940819 Jan 15 '22 at 14:35
  • time_series <- ts(Dataset1, start=1993, frequency = 4) do u think I need to change this code to something else instead also? – user17940819 Jan 15 '22 at 14:36
  • can you dput(head(Dataset1)) and provide the results. This will help to create a minimal reprobducible dataset. – Joe Erinjeri Jan 15 '22 at 14:37
  • > head(Dataset1) ï..Year ChinaGDP Yuan totalcredit bankcredit creditpercGDP creditGDPratio 1 1993-Q1 15.3 5.77 437.02 487.68 97.0 86.9 2 1993-Q2 13.5 5.74 455.20 504.50 94.7 85.4 3 1993-Q3 12.9 5.79 469.42 523.37 92.4 82.9 4 1993-Q4 14.1 5.81 521.68 581.83 95.6 85.7 5 1994-Q1 14.1 8.72 363.42 403.48 91.9 82.8 6 1994-Q2 13.3 8.70 389.01 431.06 90.0 81.2 – user17940819 Jan 15 '22 at 14:42
  • looks like head(Dataset1) rather than dput(head(Dataset1)) – Joe Erinjeri Jan 15 '22 at 14:45
  • > dput(head(Dataset1)) structure(list(ï..Year = c("1993-Q1", "1993-Q2", "1993-Q3", "1993-Q4", "1994-Q1", "1994-Q2"), ChinaGDP = c(15.3, 13.5, 12.9, 14.1, 14.1, 13.3), Yuan = c(5.77, 5.74, 5.79, 5.81, 8.72, 8.7), totalcredit = c(437.02, 455.2, 469.42, 521.68, 363.42, 389.01), bankcredit = c(487.68, 504.5, 523.37, 581.83, 403.48, 431.06), creditpercGDP = c(97, 94.7, 92.4, 95.6, 91.9, 90), creditGDPratio = c(86.9, 85.4, 82.9, 85.7, 82.8, 81.2)), row.names = c(NA, 6L), class = "data.frame") – user17940819 Jan 15 '22 at 14:50
  • try running the edits above – Joe Erinjeri Jan 15 '22 at 14:55
  • That seem to work! 1. Now my concern is, I have 115 obs for each variable so should i do all of them like that? 2. I tried plotting each variable separately, it says this error, "Error in plot(Yuan) : object 'Yuan' not found" THANK YOU SO MUCH FOR YOUR HELP, REALLY APPRECIATE IT – user17940819 Jan 15 '22 at 15:07
  • Or should I run each variable separately with the dates (using the code you just gave me), and then merge all the variables? :( – user17940819 Jan 15 '22 at 15:09