JontroPothon
JontroPothon

Reputation: 600

Taking particular tsibble data out of the data frame

I am trying to solve a very simple problem. I want to take out a particular quarter data out of a tsibble.

I have this code,

library(tidyverse) 
library(tsibble) 

x <- tsibble(
      qtr = rep(yearquarter("2010 Q1") + 0:9, 3),
      group = rep(c("x", "y", "z"), each = 10),
      value = rnorm(30),
      key = group
    )

Now I want to subset the 2010 Q1 data out,

x %>% filter(qtr == "2010 Q1")

I am getting this error message,

> x %>% filter(qtr == "2010 Q1")
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

What am I doing wrong here?

Upvotes: 1

Views: 271

Answers (1)

A. Suliman
A. Suliman

Reputation: 13135

As the error message said when we use "2010 Q1"

character string is not in a standard unambiguous format

Therefore we need to transfer "2010 Q1" into standard format before we do filtering, hence we used yearquarter

library(tidyverse) 
library(tsibble) 
x %>% filter(qtr == yearquarter("2010 Q1"))

# A tsibble: 3 x 3 [1Q]
# Key:       group [3]
      qtr group value
    <qtr> <chr> <dbl>
1 2010 Q1 x     1.51 
2 2010 Q1 y     0.919
3 2010 Q1 z     1.36 

Upvotes: 1

Related Questions