Yi
2010-Jun-25 19:00 UTC
[R] Delete rows in the data frame by limiting values in two columns
Hi, folks, Finally Friday~~ Here comes the question: x=c('germany','poor italy','usa','england','poor italy','japan') y=c('Spain','germany','usa','brazil','england','chile') s=1:6 z=3:8 test=data.frame(x,y,s,z) #Now I only concern the countries ('germany','england','brazil'). I would like to keep the rows where these three countries #are involved either in test$x OR test$y. So the result should be like as follows (I did this manually >< ): x y s z 1 germany Spain 1 3 2 poor italy germany 2 4 3 england Brazil 4 6 4 poor italy england 5 7 Any codes work for this? Thanks great in advance. [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Jun-25 19:04 UTC
[R] Delete rows in the data frame by limiting values in two columns
Try this: test[rowSums(mapply('%in%', test[c('x', 'y')], list(c('germany','england','brazil')))) > 0,] On Fri, Jun 25, 2010 at 4:00 PM, Yi <liuyi.feier@gmail.com> wrote:> Hi, folks, > > Finally Friday~~ Here comes the question: > > x=c('germany','poor italy','usa','england','poor italy','japan') > y=c('Spain','germany','usa','brazil','england','chile') > s=1:6 > z=3:8 > test=data.frame(x,y,s,z) > > #Now I only concern the countries ('germany','england','brazil'). I would > like to keep the rows where these three countries > #are involved either in test$x OR test$y. So the result should be like as > follows (I did this manually >< ): > > x y s z > 1 germany Spain 1 3 > 2 poor italy germany 2 4 > 3 england Brazil 4 6 > 4 poor italy england 5 7 > > Any codes work for this? > > Thanks great in advance. > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Erik Iverson
2010-Jun-25 19:05 UTC
[R] Delete rows in the data frame by limiting values in two columns
> x=c('germany','poor italy','usa','england','poor italy','japan') > y=c('Spain','germany','usa','brazil','england','chile') > s=1:6 > z=3:8 > test=data.frame(x,y,s,z) > > #Now I only concern the countries ('germany','england','brazil'). I would > like to keep the rows where these three countries > #are involved either in test$x OR test$y. So the result should be like as > follows (I did this manually >< ): > > x y s z > 1 germany Spain 1 3 > 2 poor italy germany 2 4 > 3 england Brazil 4 6 > 4 poor italy england 5 7 > > Any codes work for this?ss <- c("germany", "england", "brazil") subset(test, x %in% ss | y %in% ss)