将日期作为整数和时间作为R中POSIXct的因子 [英] Combine date as integer and time as factor to POSIXct in R

查看:163
本文介绍了将日期作为整数和时间作为R中POSIXct的因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了几次,我看了这些问题并遵循了这些建议。但是,我无法解决这个问题。



datetime.csv可以在 https://www.dropbox.com/s/6bvhk4kei4pg8zq/datetime.csv



我的代码看起来像:

  jd1<  -  read.csv(datetime.csv)
头(jd1)
日期时间
1 20100101 0:00
2 20100101 1:00
3 20100101 2:00
4 20100101 3:00
5 20100101 4:00
6 20100101 5:00

sapply(jd1,class)
> sapply(jd1,class)
日期时间
整数因子

jd1< - transform(jd1,timestamp = format(as.POSIXct时间)),%Y%m%d%H:%M:%S))
as.POSIXlt.character(x,tz,...)中的错误:
字符串是没有标准的明确格式

我尝试了rcs在将两列日期和时间数据转换为一个,但这似乎给出一个错误。



非常感谢任何帮助。



谢谢。

解决方案

您传递格式的格式字符串包括%S 你没有但是,从 as.POSIXct 来的错误将不会解决。您需要将格式字符串传递到那里,并删除对格式的调用函数。

 code> foo<  -  transform(jd1,timestamp = as.POSIXct(paste(Date,Time),format =%Y%m%d%H:%M))
str(foo )

将此与:

  bar<  -  transform(jd1,timestamp = as.POSIXct(paste(Date,Time),format =%Y%m%d%H:%M:%S))
str(bar)

调用格式的结果

  baz<  -  transform(jd1,timestamp = format(as.POSIXct(paste时间),格式=%Y%m%d%H:%M),format ='%Y%m%d%H:%M:%S'))
str(baz)


I know this has been asked several times and I looked at the questions and followed the suggestions. However, I couldn't solve this one.

The datetime.csv can be found on https://www.dropbox.com/s/6bvhk4kei4pg8zq/datetime.csv

My code looks like:

jd1 <- read.csv("datetime.csv")
head(jd1)
      Date Time
1 20100101 0:00
2 20100101 1:00
3 20100101 2:00
4 20100101 3:00
5 20100101 4:00
6 20100101 5:00

sapply(jd1,class)
> sapply(jd1,class)
     Date      Time 
"integer"  "factor"

jd1 <- transform(jd1, timestamp=format(as.POSIXct(paste(Date, Time)), "%Y%m%d %H:%M:%S"))
Error in as.POSIXlt.character(x, tz, ...) : 
character string is not in a standard unambiguous format

I tried the solution suggested by rcs on Converting two columns of date and time data to one but this seems to give an error.

Any help is highly appreciated.

Thanks.

解决方案

The format string you're passing to format includes %S which you don't have. But that won't fix the error since its coming from as.POSIXct. You need to pass the format string there instead and remove the call to the format function.

foo <- transform(jd1, timestamp=as.POSIXct(paste(Date, Time), format="%Y%m%d %H:%M"))
str(foo)

Compare this to:

bar <- transform(jd1, timestamp=as.POSIXct(paste(Date, Time), format="%Y%m%d %H:%M:%S"))
str(bar)

And the result of calling format:

baz <- transform(jd1, timestamp=format(as.POSIXct(paste(Date, Time), format="%Y%m%d %H:%M"), format='%Y%m%d %H:%M:%S'))
str(baz)

这篇关于将日期作为整数和时间作为R中POSIXct的因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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