简体   繁体   English

R 中的 $mix$a 和 $mix$b 是什么意思?

[英]What does $mix$a and $mix$b in R means?

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.我是 R 编程的新手,我不明白以下问题中的 $mix、$mix$a 和 $mix$b 是什么意思。

# Create a variable "mydata" - a data structure with the output presented 
# below as comments

# > 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 

This is the code that i wrote.这是我写的代码。

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

print(mydata)

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.但我不明白 $mix$a 和 $mix$b 是什么意思。

Can you please explain?你能解释一下吗?

Thanks.. :)谢谢.. :)

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$mixmydata$mix$amydata$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或者另一种选择是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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM