将字符串列表转换为表示变量的符号列表

Convert a list of strings to a list of symbols that represent variables

我有一个字符串列表,表示全局环境中的 xts 对象名称。如何将字符串列表转换为要正确格式化以插入 do.call 函数的名称列表?这样函数就合并了 xts 对象,而不仅仅是字符串的名称。

xtsNames <- list("name1", "name2", "name3")

name1 <- xts(x=1:10, order.by=Sys.Date()-1:10)
name2 <- xts(x=11:20, order.by=Sys.Date()-11:20)
name3 <- xts(x=21:30, order.by=Sys.Date()-21:30)


as.name(xtsNames) # this code fails because it does not work on a list

NewData <- do.call(rbind, xtsNames) # this code merges the strings, rather 
                                    # than the xts objects

您可以使用 rlang 包将变量名字符串转换为实际符号:

do.call( rbind, rlang::syms(xtsNames) )

如果你只想要基础 R,那么可以使用 lapply:

do.call( rbind, lapply(xtsNames, as.name) )

我不确定我是否完全理解你的问题,但你可能想在这里使用 'get'。

do.call('rbind',lapply(xtsNames, get))

旁注:要理解'get',假设x <- 2,那么如果你通过了,在控制台上get('x') , 你会得到 2 作为值 returned.

?get:

Search by name for an object (get) or zero or more objects (mget).

这将 return 如下所示的一些行:

> do.call('rbind',lapply(xtsNames, get))
           [,1]
2018-09-27   30
2018-09-28   29
2018-09-29   28
2018-09-30   27
2018-10-01   26
2018-10-02   25
2018-10-03   24