Converting data to time series... unintended transformation!

Hello everyone,

First time on this forum so hello and thanks to you all in advance. Really hope you can see the screen shot of the code I tried to include. but all I've done here is trying to convert a dataset into a timeseries. Upon doing so, R somehow has converted / transformed to all new values. Can anyone offer advice on how to fix this?

image

Hi @fixmeplease,
Welcome to the RStudio Community Forum.

Looks like your data has been imported from the CSV file incorrectly, since the commas in the second column mean that column has been imported as "character" not "numeric". You could fix that in Excel before importing, or deal with the problem in R. Another possibility is that second column is meant to be two separate columns?

suppressPackageStartupMessages(library(tidyverse))

vargrp <- data.frame(Date = c("1994-10-01","1995-01-01","1995-04-01",
                              "1995-07-01","1995-10-01","1996-01-01"),
                     GDP..mkt.prices = c("810,788", "823,972", "828,856",
                                         "832,976", "840,680", "844,208"))

vargrp
#>         Date GDP..mkt.prices
#> 1 1994-10-01         810,788
#> 2 1995-01-01         823,972
#> 3 1995-04-01         828,856
#> 4 1995-07-01         832,976
#> 5 1995-10-01         840,680
#> 6 1996-01-01         844,208
str(vargrp)
#> 'data.frame':    6 obs. of  2 variables:
#>  $ Date           : chr  "1994-10-01" "1995-01-01" "1995-04-01" "1995-07-01" ...
#>  $ GDP..mkt.prices: chr  "810,788" "823,972" "828,856" "832,976" ...
var_one <- vargrp
var_one$price <- as.numeric(gsub(",", "", var_one$GDP..mkt.prices))
var_one <- var_one[, -2]
ts_one <- ts(var_one, frequency=4, start=c(1994, 4))
ts_one
#>         Date  price
#> 1994 Q4    1 810788
#> 1995 Q1    2 823972
#> 1995 Q2    3 828856
#> 1995 Q3    4 832976
#> 1995 Q4    5 840680
#> 1996 Q1    6 844208
str(ts_one)
#>  Time-Series [1:6, 1:2] from 1995 to 1996: 1 2 3 4 5 ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : NULL
#>   ..$ : chr [1:2] "Date" "price"
var_two <- separate(vargrp, 
                    GDP..mkt.prices, 
                    into=c("price1", "price2"))

var_two$price1 <- as.numeric(var_two$price1)
var_two$price2 <- as.numeric(var_two$price2)

ts_two <- ts(var_two, frequency=4, start=c(1994, 4))
ts_two
#>         Date price1 price2
#> 1994 Q4    1    810    788
#> 1995 Q1    2    823    972
#> 1995 Q2    3    828    856
#> 1995 Q3    4    832    976
#> 1995 Q4    5    840    680
#> 1996 Q1    6    844    208
str(ts_two)
#>  Time-Series [1:6, 1:3] from 1995 to 1996: 1 2 3 4 5 6 810 823 828 832 ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : NULL
#>   ..$ : chr [1:3] "Date" "price1" "price2"

Created on 2021-06-19 by the reprex package (v2.0.0)

The documentation of ts indicates that it can accept a vector or matrix input. You're supplying a data frame input, which will get coerced to matrix with potential unintended results.

Thanks so much! yes that was indeed the problem. All good now. I had to leave it alone over the weekend for fear I might put my computer through a window... take care.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.