0

I am beginner in R software. I know the basics at least, but having problem with the datatype. So my data is weather variation data for a period of 2000-2005. When i am writing class(testdata) it is showing the class as dataframe instead of time-series. So my question is why a dataframe with dates and months of years is not a time-series data?

P.S. the data shows measurements of each day from 2000-2005.

Data looks somewhat like this:

date        respadmissions   NO2 
1.1.2001         2            5      
1.2.2001         4            6
1.3.2001         5            7
1.4.2001         6            8
hasan arshad
  • 41
  • 2
  • 6
  • A timeseries is a different class structure in R entirely. You most likely just have a data frame with a date column and a value column. Could you provide a small sample of your data, or data similar to yours? – BurlyPotatoMan Mar 29 '19 at 12:28
  • 2
    A data.frame can be transformed into a time series but is not necessarily one just because there is a date or date/time column. Check the [CRAN Task View](https://cran.r-project.org/web/views/TimeSeries.html) on time series. – Rui Barradas Mar 29 '19 at 12:31
  • 1
    Possible duplicate of [how to convert data frame into time series in R](https://stackoverflow.com/questions/29046311/how-to-convert-data-frame-into-time-series-in-r) – divibisan Mar 29 '19 at 20:21

1 Answers1

0

As pointed out in comments: timeseries is a data structure in R, with its own parameters and specifics. Your data is not a timeseries object, because you have specified it as a dataframe, which is a different structure.

You can store your data as timeseries (or convert your dataframe to time-series object) with ts() function like this:

# first making the dataframe
dat <- structure(list(NO2 = c(2,4,5,6),  respadmissions = c(2,4,5,6)), class = "data.frame", row.names = c(NA, -4L))

# making the date vector and adding it to the dataframe
dates <- c("01/01/2001", "02/01/2001","03/01/2001","04/01/2001")
ds <- as.Date(dates, "%m/%d/%Y")
dat$date <- ds

# making a time-series object with NO2-data
time_ser<-ts(matrix(dat$NO2,nrow=4),start=c(2001-01-01),frequency=1)  
Oka
  • 1,318
  • 6
  • 11
  • just for clarification, this is the secondary data that i am going to use. So i received the data as such and i have to apply the time-series analysis. Is it necessary for the data to be "dataframe" in R for applying time series analysis? – hasan arshad Apr 01 '19 at 10:31
  • Time series object actually requires `vector` or `matrix`. In the answer the code transforms `dataframe` (which is what you had) to `matrix` (which is what is needed) to get to the `timeseries` object (which is what you want). – Oka Apr 01 '19 at 10:48
  • Did I answer your question? – Oka Apr 01 '19 at 10:48