On Jul 5, 2010, at 8:54 AM, LogLord 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.



Probably not the most elegant solution. For each data row, see which one or more rows of category satisfies. Not tested for possibility of non-hit:

> for (i in 1:nrow(data)) print( category[
                      which(apply(category[, -1], 1,
function(x) {data$b[i]==x[1] & data $c[i] > x[2] & x[3] > data$c[i]})),
                                            1] )
[1] cat3
Levels: cat1 cat2 cat3 cat4 cat5 cat6
[1] cat1
Levels: cat1 cat2 cat3 cat4 cat5 cat6
[1] cat5
Levels: cat1 cat2 cat3 cat4 cat5 cat6

A couple of points. Bad practice to name variables or objects with the name "c". Also bad practice to name objects "data". Both at common R function names.

I hope this explains my problem more explicit.

Thanks!
--
View this message in context: 
http://r.789695.n4.nabble.com/Assigning-entries-to-categories-tp2272697p2278334.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.

David Winsemius, MD
West Hartford, CT

______________________________________________
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.

Reply via email to