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". i want to construct a posixlt with the combined date and time. i tried:
我必须在数据框中修改日期。日期是“16/12/2006”,时间是“17:24:00”。我想构建一个具有组合日期和时间的posixlt。我试过了:
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.
这是课程之一的功课。
1 个解决方案
#1
0
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. You can see the attributes available with something like:
对于您的第二点,$ month和$ day不可用属性。您可以通过以下方式查看可用的属性:
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。
#1
0
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. You can see the attributes available with something like:
对于您的第二点,$ month和$ day不可用属性。您可以通过以下方式查看可用的属性:
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。