Question

Bit of an unusual request, but I am trying to round up datetimes in every second column of a data frame. I think I can already identify every second column (using df[c(T,F)], but am having trouble working out how to apply the transformation to these columns.

Here is what I am currently trying to use:

for (ci in 1:ncol(df1[c(T,F)])) {
  ci<-round.POSIXt(as.Date(df[c(T,F)]),format = "%d/%m/%Y %H:%M")
}

I'm also having trouble converting the current datestamps to dates as they are stored as factors in the following format 2013/10/24 00:19:00. I have tried a number of things, including:

as.POSIXct(strptime(as.numeric((df[2], "%Y/%m/%d %H:%M:%S"))

strptime(df[2], format='%Y/%m/%d %H:%M:%S')

But I keep getting the following error:

Error in as.Date.default(a[1]) : 
  do not know how to convert 'a[1]' to class "Date"

EDIT: Reproducible example

I used dput on my dataframe to reproduce the first 3 rows and 6 columns, aplogies for the length of output (I assume this is due to the dates being stored as factors at present).

structure(list(Sample.Time..Trend.1. = structure(2:4, .Label = c("", 
                                                             "2013/10/24 00:19:00", "2013/10/24 00:49:00", "2013/10/24 01:18:59", 
                                                             "2013/10/24 01:48:59", "2013/10/24 02:18:59", "2013/10/24 02:48:59", 
                                                             "2013/10/24 03:18:59", "2013/10/24 03:48:59", class = "factor"), AHU.DJ_SATemp = c(23.5765, 
                                                                                                                          23.5814, 23.5814), Sample.Time..Trend.2. = structure(2:4, .Label = c("", 
                                                                                                                                                                                               "2013/10/24 00:19:00", "2013/10/24 00:49:00", "2013/10/24 01:18:59", 
                                                                                                                                                                                               "2013/10/24 01:48:59", "2013/10/24 02:18:59", "2013/10/24 02:48:59", 
                                                                                                                                                                                               "2013/10/24 03:18:59", "2013/10/24 03:48:59"), class = "factor"), AHU.DJ_RATemp = c(23.5814, 
                                                                                                                                                                                                                                                            23.5814, 23.4886), Sample.Time..Trend.3. = structure(1:3, .Label = c("2013/10/21 22:30:00", 
                                                                                                                                                                                                                                                                                                                                 "2013/10/21 23:00:00", "2013/10/21 23:30:00", "2013/10/22 00:00:00", 
                                                                                                                                                                                                                                                                                                                                 "2013/10/22 00:30:00", "2013/10/22 01:00:00", "2013/10/22 01:30:00", 
                                                                                                                                                                                                                                                                                                                                 "2013/10/22 02:00:00", "2013/10/22 02:30:00", "2013/10/22 03:00:00", 
                                                                                                                                                                                                                                                                                                                                 "2013/10/22 03:30:00"), class = "factor"), AHU.DJ_HWValve = c(0, 
                                                                                                                                                                                                                                                                                                                                                                                               0, 0)), .Names = c("Sample.Time..Trend.1.", "AHU.DJ_SATemp", 
                                                                                                                                                                                                                                                                                                                                                                                                                  "Sample.Time..Trend.2.", "AHU.DJ_RATemp", "Sample.Time..Trend.3.", 
                                                                                                                                                                                                                                                                                                                                                                                                                  "AHU.DJ_HWValve"), row.names = c(NA, 3L), class = "data.frame")

EDIT2: Working Code:

Finally got this working with thanks to @Henrik below. Here is the final version of the code:

library(lubridate)
try(df<- read.csv("Trends.csv"))

# convert factor versions of dates to as.POSIXct
df[c(TRUE, FALSE)] <- lapply(df[c(TRUE, FALSE)], function(x){
  as.POSIXlt(strptime(x, , format='%Y/%m/%d %H:%M:%S'))
})
str(df)

# round every second columns to nearest half-hour
df[c(TRUE, FALSE)] <- lapply(df[c(TRUE, FALSE)], function(x){
  format(as.POSIXlt(round(as.double(x)/(30*60))*(30*60),origin=(as.POSIXlt('1970-01-01'))),format='%d/%m/%Y %H:%M')
  }
)

# Loop through data frame and output results to file
for (ci in 1:ncol(df)) {
  a<-na.omit(cbind(df[ci-1],df[ci]))
  write.csv(a, paste(colnames(df[ci]), ".csv",sep = ""),quote=FALSE,row.names=FALSE)
}
Was it helpful?

Solution

For some reason your example data turned up a little bit strange for me, so I made up a small data set:

library(lubridate)

# some test data with dates as factors
tt <- as.factor(c(Sys.time(), Sys.time()))
df <- data.frame(a = tt, b = tt, c = tt, d = tt)
str(df)

# convert factor versions of dates to as.POSIXct
df[] <- lapply(df, function(x) ymd_hms(as.character(x)))
str(df)

# round every second columns to nearest minut
df[c(TRUE, FALSE)] <- lapply(df[c(TRUE, FALSE)], function(x){
  round_date(x, "minute")
}
                             )
str(df)
df
#                     a                   b                   c                   d
# 1 2013-10-29 17:26:00 2013-10-29 17:26:20 2013-10-29 17:26:00 2013-10-29 17:26:20
# 2 2013-10-29 17:26:00 2013-10-29 17:26:20 2013-10-29 17:26:00 2013-10-29 17:26:20
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top