@slot Extraction

Asked

Viewed 50 times

1

Does anyone know what a slot extraction? I’m trying to follow a script and a part of it is:

data <- data.frame 
names(data)
unique <- unique(data@data$binomial)

binomial is the column I need to use in the case that they are species names. A species in this case may have more than one row in the data.frame, therefore the application of the function unique. But I don’t understand the function of @. I saw that it is related to an Extraction slot, but I didn’t understand what it means in practice.

  • 1

    data.frame is the function that creates, good, data frames. So the first line of that code is creating another function the same but with a different name. The second instruction, names(data) must give NULL.

  • 2

    And then the third instruction makes no sense. As for the operator @ is actually related to an Extraction slot, that is, it is used in object-oriented programming. In R there is the S3 system (most used) and the S4 system, in the package methods, where this operator is used to extract components (slots) from objects. See Advanced R by Hadley Wickham.

  • I expressed myself badly using data.frame in the first line, I meant that in my script the object "data" is a data.frame, I put so to contextualize

  • 2

    Okay, so you can post the output of dput(data) or, if the df is too large, dput(head(data, 20)) question? So we get an exact copy of the data structure and we can see what is happening.

1 answer

1

There is a class of objects in R calling for S4. Contents within an object of type S4 are called slots. In the same way as the $ is used to access columns of a data.frame, you can access the slots of an object of the class S4 via the operator @.

To illustrate, let’s create a class S4 for example:

setClass("minha_classe_S4", slots = c(x = "numeric", y = "character"))

We create a class called "my_classe_S4" that contains two slots, a call x which must be numerical and another class y which needs to be text. Now let’s create an object of this class:

meu_objeto <- new("minha_classe_S4", x = 1:10, y = "texto")

To access the element x of meu_objeto, we use the @:

meu_objeto@x

Browser other questions tagged

You are not signed in. Login or sign up in order to post.