0

有点不寻常的请求,但我试图在数据帧的每一列中四舍五入日期时间。我想我已经可以识别每隔一列(使用df[c(T,F)],但无法确定如何将转换应用于这些列。

这是我目前正在尝试使用的:

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

我也无法将当前的日期戳转换为日期,因为它们以以下格式 2013/10/24 00:19:00 存储为因子。我尝试了很多事情,包括:

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

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

但我不断收到以下错误:

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

编辑:可重现的例子

我在我的数据帧上使用 dput 来重现前 3 行和 6 列,为输出长度道歉(我假设这是由于日期被存储为目前的因素)。

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:工作代码:

感谢下面的@Henrik,终于得到了这个工作。这是代码的最终版本:

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)
}
4

1 回答 1

1

出于某种原因,您的示例数据对我来说有点奇怪,所以我制作了一个小数据集:

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
于 2013-10-29T16:30:09.383 回答