Conversion csv into time series ts R Studio

Hi, I have a problem with converting my csv weekly data into time series.
Firstly R Studio correctly sees my data (part of them below):
Tydzien X50StyleAUnisexABP
1 2015-02-02 189
2 2015-02-09 191
3 2015-02-16 172
4 2015-02-23 205
5 2015-03-02 211

Data starts in 6th week of 2015 and ends in 5th week of 2019. "Tydzien" is the date and "X50StyleAUnisexABP" is the sale

I used this:
dane.ts <- ts(dane$X50StyleAUnisexABP, start = c(2015,6), end = c(2019, 5), frequency = 52)
dane.ts

However when I try to make it seen like time series, "X50StyleAUnisexABP" variable is much lower (I don't know why :frowning: )
R Studio sees (a part of) my time series this way:
dane.ts
Time Series:
Start = c(2015, 6)
End = c(2019, 5)
Frequency = 52
[1] 10 11 8 21 24 31 11 21 9 84 85 36 22 80 61 30 85 54 52 62 79 122 147 148 153 167
[27] 4 14 71 114 73 158 123 106 98 88 75 27 11 56 50 58 66 116 128 150 117

What am I doing wrong? Help me please :slight_smile: Thank you in advance

My gut says this is an "accidentally a factor" problem. You should run the line of code that reads the table, and then inspect the output. Use functions like summary(dane) and str(dane). Does the structure match what you expect?

But a reproducible example (reprex) would really help us debug with you. This thread can help you make one: FAQ: What's a reproducible example (reprex) and how do I do one? This is also the type of problem in which, by writing a reprex, you might stumble onto the cause yourself.

Also, I'd like to clear up a common confusion: R and R Studio are separate things. R often refers to both the programming language and the interpreter that runs the code. R Studio is a development tool for editing files and managing code projects.

As suggested, understanding the structure of your data frame would be a great first step.

Perhaps the "Tydzien" variable was read from original csv was into a character or factor or some other format.

Since you haven't provided a reprex (reproducible example), I will attempt to work what you you have shared here. For illustration, lets say that your data looks something like this:

dane_sample <- data.frame(Tydzien = c("2015-02-02", "2015-02-09", "2015-02-16", "2015-02-23", "2015-03-02"),
                      X50StyleAUnisexABP = c(189, 191, 172, 205, 211))

The structure of that data frame looks like this:

> str(dane_sample)
'data.frame':	5 obs. of  2 variables:
 $ Tydzien           : Factor w/ 5 levels "2015-02-02","2015-02-09",..: 1 2 3 4 5
 $ X50StyleAUnisexABP: num  189 191 172 205 211

Notice that the Tydzien variable in this example is a factor.

One option for you: utilize "mutate" function from the dplyr package to transform the this variable into a date format. Something like this:

library(tidyverse)

dane_modified <- dane_sample %>%
  mutate(Tydzien_modified = as.Date(Tydzien))
dane_modified


> dane_modified
     Tydzien X50StyleAUnisexABP Tydzien_modified
1 2015-02-02                189       2015-02-02
2 2015-02-09                191       2015-02-09
3 2015-02-16                172       2015-02-16
4 2015-02-23                205       2015-02-23
5 2015-03-02                211       2015-03-02

> str(dane_modified)
'data.frame':	5 obs. of  3 variables:
 $ Tydzien           : Factor w/ 5 levels "2015-02-02","2015-02-09",..: 1 2 3 4 5
 $ X50StyleAUnisexABP: num  189 191 172 205 211
 $ Tydzien_modified  : Date, format: "2015-02-02" "2015-02-09" "2015-02-16" "2015-02-23" ...

Notice that the newly created "Tydzien_modified" variable is now in a "Date" format.

I hope this makes sense.

The code provided by @paulina.teczak didn't use the Tydzien column:

[Added code formatting.] Of course, that's not to say this isn't a problem elsewhere in the code. Or that it doesn't have a hand in the question.

My guess is the X50StyleAUnisexABP column is being read in as a factor. The CSV might have quotation marks around those values, or they're being read as character by read.csv(), or something else entirely.

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.