Reputation: 217
I want to transforming the time stamps of a dataset into something more workable, and am new to lubridate/R.
The data originally came in %d/%m/%Y %H:%M
and would like the change this to %d %H
from the starting time.
I want to truncate the data down to DMY_H then rearrange it to Hours and Days after origin
Here is sample of the data:
time dht22_t dht11_t dht22_h dht11_h db pa treatment_hive wifi
01/09/2014 15:19 NA NA NA NA 51.75467 NA 0 1
01/09/2014 15:20 30.8 31 59.8 44 55.27682 100672 0 1
01/09/2014 15:21 30.8 31 60.3 44 54.81995 100675 0 1
01/09/2014 15:22 30.8 31 60.9 44 54.14134 100671 0 1
01/09/2014 15:23 30.8 31 61.1 44 53.88574 100672 0 1
01/09/2014 15:24 30.8 31 61.2 44 53.68800 100680 0 1
Thanks! EDIT:
The code:
df$time<-format(ymd_hms(df$time), '%d %H:%M:%S')
changed the days, but not the hours minutes seconds to a starting time or origin. Ideally, it would look like this:
time DHT22_t DHT11_t DHT22_h DHT11_h db pa hive_id treatment_hive wifi
01 15:00:00 NA NA NA NA 51.75467 NA 1 0 1
01 16:00:00 30.8 31 59.8 44 55.27682 100.672 1 0 1
01 17:00:00 30.8 31 60.3 44 54.81995 100.675 1 0 1
EDIT:
> dput(droplevels(head(Hive2)))
structure(list(time = structure(1:6, .Label = c("2014-09-01 15:25:05",
"2014-09-01 15:25:09", "2014-09-01 15:25:11", "2014-09-01 15:25:15",
"2014-09-01 15:25:18", "2014-09-01 15:25:20"), class = "factor"),
DHT22_t = c(0, 0, 0, 0, 0, 0), DHT11_t = c(0L, 31L, 31L,
31L, 31L, 31L), DHT22_h = c(0, 0, 0, 0, 0, 0), DHT11_h = c(0L,
51L, 53L, 53L, 52L, 50L), db = c(60.8, 59.4, 60.4, 59.2,
60.3, 60.2), kPa = c(NA, 100.798, 100.792, 100.791, 100.79,
100.791), hive_id = c(2L, 2L, 2L, 2L, 2L, 2L), treatment_hive = c(1L,
1L, 1L, 1L, 1L, 1L), wifi = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("time",
"DHT22_t", "DHT11_t", "DHT22_h", "DHT11_h", "db", "kPa", "hive_id",
"treatment_hive", "wifi"), row.names = c(NA, 6L), class = "data.frame")
for this code:
dt1 <- ymd_hms(Hive2$time)
v1 <- times(format(dt1, '%H:%M:%S'))
dt2<- paste(format(dt1, '%d'), v1-v1[1])
> dput(head(dt2))
c("01 0.000000e+00", "01 4.629630e-05", "01 6.944444e-05",
"01 1.157407e-04", "01 1.504630e-04", "01 1.736111e-04")
and this:
dt2.1<- paste(format(dt1, '%d %H:%M:%S'), v1-v1[1])
> dput(head(dt2.1))
c("01 15:25:05 0.000000e+00", "01 15:25:09 4.629630e-05", "01 15:25:11 6.944444e-05",
"01 15:25:15 1.157407e-04", "01 15:25:18 1.504630e-04", "01 15:25:20 1.736111e-04"
)
Upvotes: 1
Views: 891
Reputation: 887158
You could try
library(lubridate)
library(chron)
dt1 <- dmy_hm(df$time)
NOTE: Based on the example provided, the times are 15:19:00
, 15:20:00
. One reason to show dataset using dput
.
dt1
#[1] "2014-09-01 15:19:00 UTC" "2014-09-01 15:20:00 UTC"
#[3] "2014-09-01 15:21:00 UTC" "2014-09-01 15:22:00 UTC"
#[5] "2014-09-01 15:23:00 UTC" "2014-09-01 15:24:00 UTC"
v1 <- times(format(dt1, '%H:%M:%S'))
paste(format(dt1, '%d'), v1-v1[1])
#[1] "01 00:00:00" "01 00:01:00" "01 00:02:00" "01 00:03:00" "01 00:04:00"
#[6] "01 00:05:00"
Based on the updated dataset "Hive2"
dt1 <- ymd_hms(Hive2$time)
v1 <- times(format(dt1, '%H:%M:%S'))
v1
#[1] 15:25:05 15:25:09 15:25:11 15:25:15 15:25:18 15:25:20
paste(format(dt1, '%d'), v1-v1[1])
#[1] "01 00:00:00" "01 00:00:04" "01 00:00:06" "01 00:00:10" "01 00:00:13"
#[6] "01 00:00:15"
df <- structure(list(time = c("01/09/2014 15:19", "01/09/2014 15:20",
"01/09/2014 15:21", "01/09/2014 15:22", "01/09/2014 15:23", "01/09/2014 15:24"
), dht22_t = c(NA, 30.8, 30.8, 30.8, 30.8, 30.8), dht11_t = c(NA,
31L, 31L, 31L, 31L, 31L), dht22_h = c(NA, 59.8, 60.3, 60.9, 61.1,
61.2), dht11_h = c(NA, 44L, 44L, 44L, 44L, 44L), db = c(51.75467,
55.27682, 54.81995, 54.14134, 53.88574, 53.688), pa = c(NA, 100672L,
100675L, 100671L, 100672L, 100680L), treatment_hive = c(0L, 0L,
0L, 0L, 0L, 0L), wifi = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("time",
"dht22_t", "dht11_t", "dht22_h", "dht11_h", "db", "pa", "treatment_hive",
"wifi"), class = "data.frame", row.names = c(NA, -6L))
Upvotes: 1