[R] Filtering based on the occurrence
Farnoosh Sheikhi
farnoosh_81 at yahoo.com
Fri Apr 1 00:37:58 CEST 2016
Hi Jim,
Thank you tons for your help. The code worked perfectly :) Best,Farnoosh
On Wednesday, March 30, 2016 1:13 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
Hi Farnoosh,
Despite my deep suspicion that this answer will solve a useless
problem, try this:
last_subject<-0
keep_deps<-c("B","D","F")
keep_rows<-NULL
for(rowindex in 1:dim(df)[1]) {
if(df[rowindex,"Subject"] != last_subject) {
last_subject<-df[rowindex,"Subject"]
start_keeping<-0
}
if(df[rowindex,"deps"] %in% keep_deps) start_keeping<-1
if(start_keeping) keep_rows<-c(keep_rows,rowindex)
}
final<-matrix(unlist(lapply(df[keep_rows,],as.character)),ncol=3)
I find it terribly hard to ignore puzzles.
Jim
On Wed, Mar 30, 2016 at 10:52 AM, Farnoosh Sheikhi via R-help
<r-help at r-project.org> wrote:
> Hello,
> I have a data set similar to below and I wanted to keep the observations after the first occurrence of these department: "B", "D", "F".For example for ID=2, the observation with deps=B and anything after will be kept in the data. For ID=3, observations with deps=D and anything after will be included.
> Subject<- c("2", "2", "2", "3", "3", "3", "4", "4", "5", "5", "5", "5")dates<-seq(as.Date('2011-01-01'),as.Date('2011-01-12'),by = 1) deps<-c("A", "B", "C", "C", "D", "A", "F", "G", "A", "F", "A", "D")df <- data.frame(Subject, dates, deps)df
> The final data should look like this:final<-c("2 2011-01-02 B","2 2011-01-03 C","3 2011-01-05 D","3 2011-01-06 A","4 2011-01-07 F","4 2011-01-08 G","5 2011-01-10 F","5 2011-01-11 A","5 2011-01-12 D") Thank you tons for your help.
> Farnoosh
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
[[alternative HTML version deleted]]
More information about the R-help
mailing list