What does $mix$a and $mix$b in R means?

Viewed 69

we have been asked to do a small task. I am new to R programming and I don't understand what the $mix, $mix$a and $mix$b mean in the below question.

I know when the $ sign is used that means to extract a column from a dataframe or a matrix. But I don't understand what $mix$a and $mix$b means.

Can you please explain?

Thanks.. :)

2 Answers

We can extract values using $ in a named list which can be created as -

mydata <- list("Some long text", 1:5, 
              mix = list(a = 'text', b = c(first = 97, second = 98, third = 99)))
mydata

#[[1]]
#[1] "Some long text"

#[[2]]
#[1] 1 2 3 4 5

#$mix
#$mix$a
#[1] "text"

#$mix$b
# first second  third 
#    97     98     99 

You can now get mydata$mix, mydata$mix$a and mydata$mix$b.

mydata$mix
#$a
#[1] "text"

#$b
# first second  third 
#    97     98     99 

mydata$mix$a
#[1] "text"

mydata$mix$b
# first second  third 
#    97     98     99 

We can use [[ for extraction

mydata[["mix"]]
$a
[1] "text"

$b
 first second  third 
    97     98     99 
mydata[["mix"]][["a"]]
[1] "text"

Or another option is pluck

library(purrr)
pluck(mydata, "mix", "a")
[1] "text"

data

mydata <- list("Some long text", 1:5, 
              mix = list(a = 'text', b = c(first = 97, second = 98, third = 99)))
Related