Skip to content
Prev 327662 / 398502 Next

selection based on dates

Just a note:

If the dataset is not ordered, this could result in:

?set.seed(24)
?xNew<-x[sample(1:nrow(x),6,replace=FALSE),]
idxN<-which(c(TRUE,diff(as.integer(xNew[,2]))!=0))
?xNew[-idxN,]
#[1] "2/1/13" "18"???? "30"? 


xNew1<-xNew[order(xNew[,2],xNew[,1]),]
idx<-which(c(TRUE,diff(as.integer(xNew1[,2]))!=0))
?xNew1[-idx,]
#???? [,1]???? [,2] [,3]
#[1,] "1/5/13" "15" "25"
#[2,] "1/9/13" "15" "28"
#[3,] "2/5/13" "18" "35"
#[4,] "2/9/13" "18" "38"


#Same problem applies to my solution

? xNew2<- as.data.frame(xNew, stringsAsFactors=FALSE)
?xNew[with(xNew2,ave(as.numeric(as.Date(V1,"%m/%d/%y")),V2,FUN=function(x) !x%in% min(x)))!=0,] #keeps the original order of xNew
#???? [,1]???? [,2] [,3]
#[1,] "1/5/13" "15" "25"
#[2,] "2/9/13" "18" "38"
#[3,] "1/9/13" "15" "28"
#[4,] "2/5/13" "18" "35"


A.K.





----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Andras Farkas <motyocska at yahoo.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Tuesday, July 30, 2013 9:08 AM
Subject: Re: [R] selection based on dates

Hello,

Try the following.


idx <- which(c(TRUE, diff(as.integer(x[,2])) != 0))
x[-idx,]


Also, note that in constructs such as
a <-c("1/1/13",15,20)
both 15 and 20 are coerced to character. So your matrix is a matrix of 
chars. For different types of data, use data.frames

Hope this helps,

Rui Barradas

Em 30-07-2013 13:13, Andras Farkas escreveu:
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.