[R] Assigning entries to categories

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jul 5 17:20:50 CEST 2010


On Mon, Jul 5, 2010 at 8:54 AM, LogLord <nils.schoof at web.de> wrote:
>
> OK, thanks for the help!
>
> Here a more complex example:
>
> a=c("x","y","z")
> b=c(8,14,19)
> c=c(200010,535388,19929)
> data=data.frame(a,b,c)
>
> d=c("cat1","cat2","cat3","cat4","cat5","cat6")
> b1=c(14,5,8,20,19,1)
> c_start=c(500000,500000,200000,200000,18000,600000)
> c_stop=c(550000,550000,201000,201000,20000,700000)
> category=data.frame(d,b1,c_start,c_stop)
>
>
> Again I want to create a new variable, which automatically assigns the
> category to the data based on matching b = b1 and c  >= c_start and
> <=c_stop.
>

Try this:

> library(sqldf)
>
> sqldf("select data.*, d from data, category where data.b = category.b1 and c >= c_start and c <= c_stop")
  a  b      c    d
1 x  8 200010 cat3
2 y 14 535388 cat1
3 z 19  19929 cat5



More information about the R-help mailing list