[R] Delete rows in the data frame by limiting values in two columns

Erik Iverson eriki at ccbr.umn.edu
Fri Jun 25 21:05:56 CEST 2010


> 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)



More information about the R-help mailing list