我转换了两个时间变量" Interaction2"和" Start2"到一周的格式,以便我可以按周汇总我的数据集。我想创建第三个变量" Weeks"这是" Interaction2"之间的区别。和" Start2"。我使用以下命令将时间变量转换为年,月,周(而不是年,月,日)的标准日期格式:
d1$Interaction2<-format(d1$Interaction,'%Y-%m-%U')
d1$Start2<-format(d1$Start,'%Y-%m-%U')
&#34; Interaction2&#34;的结果和&#34; Start2&#34;似乎格式正确,但它们是字符变体。我使用difftime函数来获取差异但结果是小数:
d1$Weeks<-difftime(d1$Interaction2,d1$Start2,units='weeks')
结果不应该是整数吗? difftime命令是将最后两位数字解释为一天而不是一周?我如何获得差异作为&#34; Interaction2&#34;之间的周数。一周和&#34; Start2&#34;周?
structure(list(Interaction2 = c("2015-02-06", "2015-02-08", "2015-03-09",
"2015-03-11", "2015-03-12"), Start2 = c("1995-04-16", "1995-04-16",
"1995-04-16", "1995-04-16", "1995-04-16"), Weeks = structure(c(1033.72023809524,
1034.00595238095, 1038.14285714286, 1038.42857142857, 1038.57142857143
), units = "weeks", class = "difftime")), .Names = c("Interaction2",
"Start2", "Weeks"), row.names = c(NA, 5L), class = "data.frame")
在运行差异命令之前,我还尝试使用strptime转换字符变量:
d1$Interaction3<-strptime(as.character(d1$Interaction2),"%Y%m%U")
d1$Start3<-strptime(as.character(d1$Start2),"%Y%m%U")
d1$Weeks<-difftime(d1$Interaction3,d1$Start3,units='weeks')
但这导致了&#34; Interaction3&#34;,&#34; Start3&#34;和&#34;周&#34;变量:
structure(list(Interaction2 = c("2015-02-06", "2015-02-08", "2015-03-09",
"2015-03-11", "2015-03-12"), Start2 = c("1995-04-16", "1995-04-16",
"1995-04-16", "1995-04-16", "1995-04-16"), Weeks = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), units = "weeks", class = "difftime"),
Start3 = structure(list(sec = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), min = c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_), hour = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_), mday = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), mon = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), year = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), wday = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), yday = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), isdst = c(-1L,
-1L, -1L, -1L, -1L), zone = c("", "", "", "", ""), gmtoff = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst",
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), Interaction3 = structure(list(
sec = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), min = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_), hour = c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_), mday = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_), mon = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_),
year = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_), wday = c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_), yday = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_), isdst = c(-1L,
-1L, -1L, -1L, -1L), zone = c("", "", "", "", ""), gmtoff = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst",
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt"))), .Names = c("Interaction2",
"Start2", "Weeks", "Start3", "Interaction3"), row.names = c(NA,
5L), class = "data.frame")
答案 0 :(得分:0)
尝试这个(添加单位参数),这取决于difftime隐式地将字符变量强制转换为数字变量(POSIXct)并取数字差异:
> difftime( mydf$Interaction2,mydf$Start2, units="weeks")
Time differences in weeks
[1] 1033.720 1034.006 1038.143 1038.429 1038.571