[R] Delete rows from data.frame matching a certain criteria

Petr PIKAL petr.pikal at precheza.cz
Fri Mar 2 10:42:56 CET 2012


Hi

my favourite would be

test$v[which(test$pattern==1)]<-NA

Regards
Petr


> Hi,
> 
> On Mar 1, 2012, at 12:38 PM, Sarah Goslee wrote:
> 
> > Hi,
> > 
> > On Thu, Mar 1, 2012 at 11:11 AM, mails <mails00000 at gmail.com> wrote:
> >> Hello,
> >> 
> >> 
> >> consider the following data.frame:
> >> 
> >> test <- data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
> >> c(1,1,NA,1,NA))
> >> 
> 
> < snip >
> 
> >> So basically the result should look like this:
> >>> test
> >>  n v pattern
> >> 1  1     NA       1
> >> 2  2     NA      1
> >> 3  3     7      NA
> >> 4  4     NA       1
> >> 5  5     3      NA
> > 
> >> So far, I solved it by creating subsets and using merge but it turns 
out to
> >> be super slow. Is there a way to do that
> >> with the apply function?
> > 
> > Far too much work. What about:
> > 
> >> test$v <- ifelse(test$pattern == 1, NA, v)
> >> test
> >  n  v pattern
> > 1 1 NA       1
> > 2 2 NA       1
> > 3 3 NA      NA
> > 4 4 NA       1
> > 5 5 NA      NA
> 
> Actually that doesn't work because of those pesky missing values. You 
need
> 
> test <-  transform(test, v = ifelse(pattern == 1 & !is.na(pattern), NA, 
v))
> 
> Best,
> Ista
> 
> > 
> > 
> > -- 
> > Sarah Goslee
> > http://www.functionaldiversity.org
> > 
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list