mi chiedo qual è la differenza tra il "estrazione componente/slot" (via ?Syntax
) operatori $
(simbolo di dollaro) e @
(al simbolo) in R.
Ecco un esempio con $
:
yo=data.frame(c(1:10), c(rnorm(10,0,1)))
yo$c.1.10.
stampe:
[1] 1 2 3 4 5 6 7 8 9 10
[email protected]
Error: trying to get slot "c.1.10." from an object (class "data.frame") that is not an S4 object
Ecco un esempio con @
:
setClass("track", representation(x="numeric", y="numeric"))
myTrack <- new("track", x = -4:4, y = exp(-4:4))
[email protected]
stampe:
[1] -4 -3 -2 -1 0 1 2 3 4
myTrack$x
Error in myTrack$x : $ operator not defined for this S4 class
In entrambi i casi, perché fa un lavoro e non l'altro?
Un altro esempio è la seguente dal pacchetto SoDA
in R, nella funzione geoXY
:
library(SoDA)
xy <- geoXY([email protected], [email protected], unit = 1000)
plot(xy[,1], xy[,2], asp = 1)
@ è specifico per gli oggetti S4. – joran