数据框中每第二列的舍入日期 [英] Rounding Date in every second column of data frame

查看:106
本文介绍了数据框中每第二列的舍入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个不寻常的请求,但是我试图在数据帧的第二列中四舍五入日期时间。我想我已经可以确定每隔两列了(使用 df [c(T,F)] ,但是在确定如何将转换应用于这些列时遇到了麻烦。 / p>

这是我当前要使用的内容:

  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')

但我一直收到以下错误:

  as错误。 Date.default(a [1]):
不知道如何将 a [1]转换为日期类

编辑:可复制示例



我在数据帧上使用dput来复制前3行和6列,对于输出的长度表示歉意(我假设这是由于存储了日期)

 结构(列表(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),类= 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),类= data.frame)

EDIT2:工作代码:



最后,由于下面的@Henrik,使此工作成为可能。这是代码的最终版本:

 库(lubridate)
try(df<-read.csv( Trends.csv))

#将日期的因子版本转换为as.POSIXct
df [c(TRUE,FALSE)]<-lapply(df [c(TRUE,FALSE )],函数(x){
as.POSIXlt(strptime(x,,format ='%Y /%m /%d%H:%M:%S'))
})
str(df)

#每秒钟将第二列舍入到最近的半小时
df [c(TRUE,FALSE)]<-lapply(df [c(TRUE,FALSE )],函数(x){
格式(as.POSIXlt(round(as.double(x)/(30 * 60))*(30 * 60),origin =(as.POSIXlt('1970- 01-01'))),format ='%d /%m /%Y%H:%M')
}


#遍历数据帧和输出结果归档到
中,用于(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)
}


解决方案

由于某种原因,您的示例数据对我来说有点奇怪,所以我组成了小数据设置:

 库(润滑)

#一些日期为因子
的测试数据tt<-as.factor(c(Sys.time(),Sys.time()))
df<-data.frame(a = tt,b = tt,c = tt,d = tt )
str(df)

#将日期的因子版本转换为as.POSIXct
df []<-lapply(df,function(x)ymd_hms(as.character (x)))
str(df)

#每隔第二列舍入到最近的整数
df [c(TRUE,FALSE)]<-lapply(df [c (TRUE,FALSE)],函数(x){
round_date(x, minute)
}

str(df)
df
#abcd
#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


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

解决方案

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

这篇关于数据框中每第二列的舍入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆