【问题标题】:Select the value of dictionary of a vector in R选择R中向量字典的值
【发布时间】:2022-11-20 16:11:36
【问题描述】:

我正在写 R,我用列表(或 Python 中的字典)存储我的数据, 所以这是我想做的事情: category 给定了,a给定了,希望能做出'预期的输出' (a中只有一个元素时可以,a中有多个元素时失败) 谢谢您的帮助!

category <- list(
'one' = 'AA',
'two' = 'BB',
'three' = 'CC',
'four' = 'DD')

a = c('three','two')
expected output:
c('CC','BB')

【问题讨论】:

  • 你必须使用列表吗?或者你可以使用命名向量,例如category &lt;- c('one' = 'AA', 'two' = 'BB')。然后你可以做category[ a ]
  • 你能改一下你在找什么吗?我的理解是你要获取类的元素是这样的吗?

标签: r list


【解决方案1】:

[] 括号是你要找的吗?

category <- list(
  'one' = 'AA',
  'two' = 'BB',
  'three' = 'CC',
  'four' = 'DD')

a = c('three','two')
category[a]

输出:

$three
[1] "CC"

$two
[1] "BB"

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 1970-01-01
    • 2023-01-12
    • 2016-01-11
    • 2016-04-14
    • 1970-01-01
    • 2020-04-06
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多