我正在尝试在包含日期的一系列列上应用diff()。我对date1-date2,date2-date3等
之间的区别感兴趣我感兴趣的是:
我可以在一系列日期上使用diff()(例如在第一行 - > diff(unlist(df1[1,]))
)。我只需要在每行应用这个,我猜使用apply(),但出于某种原因我无法解决这个问题。有些日期不见了,这在我的研究中是允许的。
希望这对你们来说很容易......
df <- structure(list(date1 = structure(c(-10871, -13634, -15937, -15937,
-290, -2323), class = "Date"), date2 = structure(c(16678, NA,16037, 16659,
16538, 16626), class = "Date"), date3 = structure(c(16685,16688, NA, 16659,
16568, 16672), class = "Date"), date4 = structure(c(16701, 16695, 16670,
16661, 16582, 16672), class = "Date"), date5 = structure(c(16709, 16695,
16661, 16667, 16619, 16692), class = "Date")), .Names = c("date1","date2",
"date3", "date4", "date5"), row.names = c("2", "3", "4", "5", "6", "7"),
class = "data.frame")
df
答案 0 :(得分:1)
您可以尝试这样的事情:
apply(df, 1, function(x) identical(sort(as.Date(x)), as.Date(x[!is.na(x)])))
它提供输出,表示特定行日期是否按排序顺序。
2 3 4 5 6 7
TRUE TRUE FALSE TRUE TRUE TRUE
答案 1 :(得分:1)
我认为这将更简单,更快速地处理:
dflong <- transform(
stack(lapply(df, as.numeric)),
date = as.Date(values,origin="1970-01-01"),
group = seq_len(nrow(df)),
ind = NULL,
values = NULL
)
dflong <- dflong[order(dflong$group),]
dflong$daysdiff <- with(dflong,
ave(as.numeric(date), group, FUN=function(x) c(NA,diff(x)) )
)
# date group daysdiff
#1 1940-03-28 1 NA
#7 2015-08-31 1 27549
#13 2015-09-07 1 7
#19 2015-09-23 1 16
#25 2015-10-01 1 8
#2 1932-09-03 2 NA
#8 <NA> 2 NA
#14 2015-09-10 2 NA
aggregate(daysdiff ~ group, data=dflong, function(x) any(x < 0, na.rm=TRUE) )
# group daysdiff
#1 1 FALSE
#2 2 FALSE
#3 3 TRUE
#4 4 FALSE
#5 5 FALSE
#6 6 FALSE