I have a doubt about your New table especially the 3rd row: Since after "test1" , the test fails, i guess 4,5 should be NA dat1<-read.table(text=" Device,first_failing_test,test1,test2,test3,test4,test5 1,test2,1,2,3,4,5 2,test4,2,3,4,5,6 3,test1,3,4,5,6,7 ",sep=",",header=TRUE,stringsAsFactors=FALSE)
res<-do.call(rbind,lapply(seq_len(nrow(dat1)),function(i) {indx<-colnames(dat1[i,])[-c(1:2)]%in% dat1[i,2]; indx1<- indx[-length(indx)];dat1[i,-c(1:2)][as.logical(cumsum(c(FALSE,indx1)))]<-NA; dat1[i,] })) res # Device first_failing_test test1 test2 test3 test4 test5 #1 1 test2 1 2 NA NA NA #2 2 test4 2 3 4 5 NA #3 3 test1 3 NA NA NA NA A.K. ----- Original Message ----- From: Ala' Jaouni <ajao...@gmail.com> To: r-help@r-project.org Cc: Sent: Monday, May 27, 2013 10:40 AM Subject: [R] Stop on fail using data manipulation Hello, I have a data set with test results for multiple devices (rows). I also have an index (column) that stores the first failing test for each device. I need to remove the results for all the tests that come after the first failing test. Example of a data table: Device,first_failing_test,test1,test2,test3,test4,test5 1,test2,1,2,3,4,5 2,test4,2,3,4,5,6 3,test1,3,4,5,6,7 New table: Device,first_failing_test,test1,test2,test3,test4,test5 1,test2,1,2,na,na,na 2,test4,2,3,4,5,na 3,test1,3,4,5,na,na Ideally I need to pass the first table as an argument to a function and get back the second table. Any idea how this can be done in R? Thanks [[alternative HTML version deleted]] ______________________________________________ R-help@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. ______________________________________________ R-help@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.