user5947793
user5947793

Reputation:

Analyze time series in R (with time)

I'm a newbie with R.

I have a dataset:

  meter    measurement    datetime                value
1 tv       EnergyUsed     2015-04-27  12:29:48    0.0
2 tv       EnergyUsed     2015-04-27  12:29:48    0.0
3 tv       EnergyUsed     2015-04-27  12:31:48    0.0
4 tv       EnergyUsed     2015-04-27  12:41:48    0.0
5 tv       EnergyUsed     2015-04-27  13:01:48    0.0

And I want to make a few subsets:

  1. Subsets filtered per month (So all the data from e.g. April)

  2. Subset filtered per dag (So all the data from every day)

I tried a few things (that doesn't work) and Googled a lot:

I hope this is not a duplicate, because the other sources wouldn't help. If something is unclear, please let me know. I'm just starting with R.

Upvotes: 0

Views: 107

Answers (2)

Dave2e
Dave2e

Reputation: 24089

In order to subset your data by either month, day or weekday it is best to convert the columns into a date/time class. Assuming you read your data into a dataframe (named df) from a .csv file, then them datetime is most likely a character string. To convert to a date/time class:

df$datetime <- as.POSIXct(df$datetime, format="%Y-%m-%d %H:%M:%S")

Once the column is in POSIX class, then you can subset using the

months(df$datetime)
weekdays(df$datetime)

I suggest reading the help pages and researching POSIXct, POSIXlt, strptime, cut.POSIXt as a start.
R can seem a bit quirky concerning dates and times but once you get used to it, it is a very powerful function.

Upvotes: 1

Adam Quek
Adam Quek

Reputation: 7163

Convert your datetime as date

(x<-as.Date(dat$datetime))
[1] "2015-04-27" "2015-04-27" "2015-04-27" "2015-04-27" "2015-04-27"

(1) Insert month field into your dataset and subset by month

dat$mm <- format(x, "%m")
subset(dat, mm=="04")
  meter measurement            datetime value   yyyymmdd mm
1    tv  EnergyUsed 2015-04-27 12:29:48     0 2015-04-27 04
2    tv  EnergyUsed 2015-04-27 12:29:48     0 2015-04-27 04
3    tv  EnergyUsed 2015-04-27 12:31:48     0 2015-04-27 04
4    tv  EnergyUsed 2015-04-27 12:41:48     0 2015-04-27 04
5    tv  EnergyUsed 2015-04-27 13:01:48     0 2015-04-27 04

(2) Insert combined yymmdd field and subset by specific day

dat$day <- format(x, "%y%m%d")
subset(dat, day=="150427")

  meter measurement            datetime value   yyyymmdd mm    day
1    tv  EnergyUsed 2015-04-27 12:29:48     0 2015-04-27 04 150427
2    tv  EnergyUsed 2015-04-27 12:29:48     0 2015-04-27 04 150427
3    tv  EnergyUsed 2015-04-27 12:31:48     0 2015-04-27 04 150427
4    tv  EnergyUsed 2015-04-27 12:41:48     0 2015-04-27 04 150427
5    tv  EnergyUsed 2015-04-27 13:01:48     0 2015-04-27 04 150427

Upvotes: 0

Related Questions