dae
dae

Reputation: 39

When to use @ vs $ in R?

My understanding is that "@" accesses the different containers of data within an overarching class object, while "$" is used to access a specific variable within that container.

e.g.

bmi <- atlas1006@sam_data$bmi_group

I believe I'm accessing the bmi_group variable in the sam_data container that's in the atlass1006 object.

Is this use of "@" and "$" universally true, or are there other applications of "@" and "$" in R?

Thanks!

Upvotes: 2

Views: 652

Answers (1)

ashwin agrawal
ashwin agrawal

Reputation: 1611

Generally the $ is used to extract details from S3 object and also used on vectors, matrices, arrays and lists to extract or replace parts. in R and @ is used to extract the contents of a slot in a object with a formal (S4) class structure.

You can type the below in R to know more information:

?'@'
Extract or Replace A Slot

Description

Extract or replace the contents of a slot in a object with a formal (S4) class structure.

Usage

object@name
object@name <- value
Arguments

object  
An object from a formally defined (S4) class.
name    
The character-string name of the slot, quoted or not. Must be the name of a slot in the definition of the class of object.
value   
A replacement value for the slot, which must be from a class compatible with the class defined for this slot in the definition of the class of object.
Details

These operators support the formal classes of package methods, and are enabled only when package methods is loaded (as per default).
......

Upvotes: 1

Related Questions