[R] Data frame referencing?
Sander Oom
slist at oomvanlieshout.net
Fri Aug 4 13:48:47 CEST 2006
Dear R users,
When you do:
> x <- rnorm(10)
> y <- rnorm(10)
> z <- rnorm(10)
> a <- data.frame(x,y,z)
> a$x
[1] 1.37821893 0.21152756 -0.55453182 -2.10426048 -0.08967880 0.03712110
[7] -0.80592149 0.07413450 0.15557671 1.22165341
Why does this not work:
> a[a$y>0.5,y] <-1
Error in "[<-.data.frame"(`*tmp*`, a$y > 0.5, y, value = 1) :
only 0's may be mixed with negative subscripts
While this works:
> a[a$y>0.5,2] <-1
> a
x y z
1 1.37821893 -1.0887363 1.7340522
2 0.21152756 -0.7256467 -1.3165373
3 -0.55453182 1.0000000 -2.1116072
4 -2.10426048 -0.4898596 -1.5863823
5 -0.08967880 1.0000000 -0.9139706
6 0.03712110 1.0000000 -1.3004970
7 -0.80592149 -0.7004193 -0.1958059
8 0.07413450 1.0000000 -1.3574303
9 0.15557671 -0.3335407 -2.1991236
10 1.22165341 1.0000000 -0.7576708
For a complex loop I would prefer to reference the right colomn by name,
not by number! Now, when the colomns change, I need to check my code to
make sure that the right colomns are referenced.
Suggestions much appreciated!
Thanks in advance,
Sander.
More information about the R-help
mailing list