简体   繁体   English

如何在R中的posixlt中设置日,月和年?

[英]how to set the day, month and year in a posixlt in r?

i have to fix a date in a data frame. 我必须在数据框中确定日期。 the date comes in as "16/12/2006", and the time comes in as "17:24:00". 日期为“ 16/12/2006”,时间为“ 17:24:00”。 i want to construct a posixlt with the combined date and time. 我想用合并的日期和时间构造一个posixlt。 i tried: 我试过了:

fixTime2<-function(date,time) { # replaces the date in time with the parameter date.
    fixed<-time
    fixed$mon<-as.numeric(format(date,"%m"))
    fixed$mday<-as.numeric(format(date,"%d"))
    fixed$year<-as.numeric(format(date,"%Y"))-1900
    return(fixed)
}
testFixTime2<-function() {
    date<-as.Date("16/12/2006", format = "%d/%m/%Y")
    str(date)
    time<-strptime("17:24:00","%H:%M:%S")
    str(time)
    fixed<-fixTime2(date,time)
    str(fixed)
    return(fixed)
}

when i run the program, i get: 当我运行程序时,我得到:

> source("1.R")
> f<-testFixTime2()
 Date[1:1], format: "2006-12-16"
 POSIXlt[1:1], format: "2018-07-17 17:24:00"
 POSIXlt[1:1], format: "2007-01-16 17:24:00"

the year is off by one, and the day and month are incorrect. 年份相隔一年,日期和月份不正确。

i have tried $month, and $day but they do not seem to work either. 我曾尝试$ month和$ day,但它们似乎也不起作用。

is there an easier way to construct a posixlt? 有没有更简单的方法来构造posixlt?

thanks 谢谢

i ended up with: 我最终得到了:

require(RUnit)
fixTime<-function(date,time) { # replaces the date in time with the paramer date.
    as.POSIXlt(sprintf("%s %s",date,time),format="%d/%m/%Y %H:%M:%S")
}
fixTime2<-function(month,day,year,hours,minutes,seconds) {
    print("in function fixTime")
    print(year)
    date<-paste0(c(day,month,year),collapse="/")
    time<-paste0(c(hours,minutes,seconds),collapse=":")
    fixed<-fixTime(date,time)
}
test.fixTime<-function() {
    month<-"12"
    day<-"16"
    year<-"2006"
    hours<-"17"
    minutes<-"24"
    seconds<-"00"
    print(year)
    fixTime2(month,day,year,hours,minutes,seconds)
    #fixed<-fixTime(date,time)
    checkEquals(as.numeric(month),fixed$mon+1)
    checkEquals(as.numeric(day),fixed$mday)
    checkEquals(as.numeric(year),fixed$year+1900)
    checkEquals(as.numeric(hours),fixed$hour)
    checkEquals(as.numeric(minutes),fixed$min)
    checkEquals(as.numeric(seconds),fixed$sec)
}

this is homework for one of the coursera r courses. 这是一门课程的家庭作业。

If you have individual strings, then 如果您有单独的字符串,那么

as.POSIXlt(paste(date, time), format="%d/%m/%Y %H:%M:%S")

should work. 应该管用。 Example: 例:

as.POSIXlt(paste("16/12/2006", "17:24:00"), format="%d/%m/%Y %H:%M:%S")
# [1] "2006-12-16 17:24:00"

For your second point, $month and $day aren't available attributes. 对于第二点, $month$day不是可用属性。 You can see the attributes available with something like: 您可以看到类似以下内容的可用属性:

dput(as.POSIXlt(Sys.time()))
# structure(list(sec = 48.7993450164795, min = 17L, hour = 0L, 
#     mday = 18L, mon = 6L, year = 118L, wday = 3L, yday = 198L, 
#     isdst = 1L, zone = "PDT", gmtoff = -25200L), .Names = c("sec", 
# "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
# "zone", "gmtoff"), class = c("POSIXlt", "POSIXt"), tzone = c("", 
# "PST", "PDT"))

showing that what you really need are $mon and $mday . 表明您真正需要的是$mon$mday

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM