我有以下数据框:
Date1 Date2 Date3 Date4 Date5
1 25 April 2005 10 May 2006 28 March 2007 14 November 2007 1 April 2008
2 25 April 2005 10 May 2006 28 March 2007 14 November 2007 1 April 2008
3 29 January 2008 4 December 2008 6 April 2009 1 March 2010 NA
4 29 January 2008 4 December 2008 6 April 2009 1 March 2010 1 February 2010
5 29 January 2008 4 December 2008 6 April 2009 1 March 2010 1 February 2010
6 29 January 2008 4 December 2008 6 April 2009 NA NA
以下载体:
1 01/09/2004
2 20/03/2007
3 16/09/2009
4 16/09/2009
5 15/07/2008
6 16/09/2009
我想计算数据框每行中与向量中的日期相同或日期之前的日期。例如,对于第一行,计数应为零,因为所有日期都在向量中的相应日期之后。
任何人都知道如何做到这一点?
这是dput()命令的输出,这样你们就可以更容易地将数据读入R中进行测试(如果你想):
数据帧:
structure(c(" 25 April 2005 ", " 25 April 2005 ", " 29 January 2008 ",
" 29 January 2008 ", " 29 January 2008 ", " 29 January 2008 ",
" 10 May 2006 ", " 10 May 2006 ", " 4 December 2008 ", " 4 December 2008 ",
" 4 December 2008 ", " 4 December 2008 ", " 28 March 2007 ",
" 28 March 2007 ", " 6 April 2009 ", " 6 April 2009 ", " 6 April 2009 ",
" 6 April 2009 ", " 14 November 2007 ", " 14 November 2007 ",
" 1 March 2010 ", " 1 March 2010 ", " 1 March 2010 ", " 1 March 2010 ",
" 1 April 2008 ", " 1 April 2008 ", " 1 February 2010 ", " 1 February 2010 ",
" 1 February 2010 ", " 1 February 2010 "), .Dim = c(6L, 5L), .Dimnames = list(
c("1", "2", "3", "4", "5", "6"), c("Rep1", "Rep2", "Rep3",
"Rep4", "Rep5")))
载体:
c("01/09/2004", "20/03/2007", "16/09/2009", "16/09/2009", "15/07/2008",
"16/09/2009")
答案 0 :(得分:3)
如果data.frame被称为m
和向量v
,那么简单
rowSums(m<=v)
应该这样做(这是有效的,因为m
由R表示为粘合了以下列的向量,v
将被回收)。不过,首先要确保所有日期都是POSIXct
或Date
s;有关转化本身的信息,请参阅此question。
答案 1 :(得分:2)
第一件事:你真的必须把所有东西都变成Dates,这可能有点棘手。我在矩阵中读取数据,将矢量读作vect。然后:
vect <- as.Date(vect,format="%d/%m/%Y")
# Due to the apart nature of the Date class, the normal apply-solutions
# don't give the result you're looking for.
Data <- as.data.frame(Data)
for (i in 1:ncol(Data)){
Data[,i] <- as.Date(Data[,i],format="%d %B %Y")
}
> apply(Data,2,"<=",vect)
Rep1 Rep2 Rep3 Rep4
[1,] FALSE FALSE FALSE FALSE
[2,] TRUE TRUE FALSE FALSE
[3,] TRUE TRUE TRUE FALSE
[4,] TRUE TRUE TRUE FALSE
[5,] TRUE FALSE FALSE FALSE
[6,] TRUE TRUE TRUE FALSE
> rowSums(apply(Data,2,"<=",vect))
[1] 0 2 3 3 1 3