I wonder what is the difference between the "component/slot extraction" (via ?Syntax
) operators $
(dollar sign) and @
(at symbol) in R.
Here's an example with $
:
yo=data.frame(c(1:10), c(rnorm(10,0,1)))
yo$c.1.10.
prints:
[1] 1 2 3 4 5 6 7 8 9 10
yo@c.1.10.
Error: trying to get slot "c.1.10." from an object (class "data.frame") that is not an S4 object
Here's an example with @
:
setClass("track", representation(x="numeric", y="numeric"))
myTrack <- new("track", x = -4:4, y = exp(-4:4))
myTrack@x
prints:
[1] -4 -3 -2 -1 0 1 2 3 4
myTrack$x
Error in myTrack$x : $ operator not defined for this S4 class
In either case, why does one work and not the other?
Another example is the following from the SoDA
package in R, in the function geoXY
:
library(SoDA)
xy <- geoXY(gpsObject1@latitude, gpsObject1@longitude, unit = 1000)
plot(xy[,1], xy[,2], asp = 1)