Reputation: 369
I have a list of strings that represent xts object names in the global environment. How can i convert the list of strings into a list of names to be formatted properly to insert into a do.call function? That way the function merges the xts objects, rather than just the names of strings.
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
Upvotes: 2
Views: 1242
Reputation: 11128
I am not sure if I completely understood your question, but you may want to use 'get' over here.
do.call('rbind',lapply(xtsNames, get))
On a side note: To understand 'get', suppose x <- 2, then if you pass, get('x') on the console, you will get 2 as value returned.
?get:
Search by name for an object (get) or zero or more objects (mget).
This will return some of the rows like below:
> 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
Upvotes: 3
Reputation: 13731
You can use the rlang
package to convert strings of variable names to actual symbols:
do.call( rbind, rlang::syms(xtsNames) )
If you want just base R, then it can be done using lapply
:
do.call( rbind, lapply(xtsNames, as.name) )
Upvotes: 3