[R] by() subset by factor gives unexpected results
Myles English
mylesenglish at gmail.com
Sat Aug 5 10:57:03 CEST 2017
I am having trouble understanding how the 'by' function works. Using
this bit of code:
i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=c("red","blue","blue"))
j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=c('red','blue','green'))
plot(0, 0, type="n", xlim=c(0,4), ylim=c(0,1))
by(i, i$B, function(s){ points(s$x, s$y, col=s$B) })
by(j, j$B, function(s){ points(s$x, s$y, col=s$B) })
I would have expected the point at (1,1) to be coloured red. When
plotted, this row is indeed red:
> i[1,]
x y B
1 1 0 red
however, this next point is green on the plot even though I would like
it to be red:
> j[1,]
x y B
1 1 1 red
How can I achieve that?
Myles
More information about the R-help
mailing list