有点不寻常的请求,但我试图在数据帧的每个第二列中舍入日期时间。我想我已经可以识别每一个第二列(使用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列,输出长度为aplogies(我假设这是由于日期存储为当前因素)。
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)
}
答案 0 :(得分: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