【问题标题】:Adding objects containing characters to a list R将包含字符的对象添加到列表 R
【发布时间】:2020-04-09 21:18:36
【问题描述】:

我在全局环境中保存了多个以“hello”开头的列表。例如

hello_List1
    chr "hello1"
    chr "hello2"

hello_List2
    chr "hello1234"
    chr "hello24"

我想创建一个包含所有以“hello”开头的对象列表的值的列表

例如我想要这个形成

listName
   chr "hello1"
   chr "hello2"
   chr "hello1234"
   chr "hello24"

请指导我如何进行此操作

谢谢

【问题讨论】:

    标签: r list object environment-variables


    【解决方案1】:
    lst <- mget(grep('hello', ls(), value = TRUE), envir = globalenv()) 
    do.call('rbind', unlist(lst, recursive = FALSE, use.names = FALSE))
         [,1]      
    [1,] "hello1"  
    [2,] "hello2"  
    [3,] "hello234"
    [4,] "hello24"
    

    【讨论】:

    • 这是完美的感谢,刚刚添加 as.list(...) 使其成为一个列表
    • 如果您需要将输出作为列表,则不需要do.call,只需执行unlist(lst, recursive = FALSE)
    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 2015-10-10
    • 1970-01-01
    • 2017-09-18
    • 2016-11-08
    • 2016-10-23
    • 1970-01-01
    • 2020-04-05
    相关资源
    最近更新 更多