Melvin Otieno
Melvin Otieno

Reputation: 79

How to import data from excel into R as time series

I have this excel data sheet that I want to import into R as a time series object. However, i am not sure of how to go about it.

enter image description here

How would i import this data into R?

Upvotes: 0

Views: 1581

Answers (1)

Cettt
Cettt

Reputation: 11981

first you cam use the openxlsx package to read the data:

library(openxlsx)
mydata <- read.xlsx("path to your xlsx")

Afterwards you can use the tidyverse package to transform your data to a ts object:

library(tidyverse)
mydata %>%
  mutate(Month = 1:n()) %>% 
  gather(Year, value, -Month) %>% 
  arrange(Year, Month) %>%
  {ts(.$value, start = c(2013, 1), frequency = 12)}

Upvotes: 1

Related Questions