Reputation: 6363
I've tried many things listed on this site, but nothing has yet answered this problem as far as I can tell.
I have a list of characters that specify object names within my workspace. Each of these objects represent separate columns that I would like to cbind
into a new data frame.
I can do this manually very easily. However, I am having trouble getting the character list to convert into an object list bound together as columns in a new data frame. Here is the structure of the list:
list("ID", "ID1", "ID2", "ID3", "Year", "Month", "Element", "Value1",
"MFlag1", "QFlag1", "SFlag1", "Value2", "MFlag2", "QFlag2",
"SFlag2", "Value3", "MFlag3", "QFlag3", "SFlag3", "Value4",
"MFlag4", "QFlag4", "SFlag4", "Value5", "MFlag5", "QFlag5",
"SFlag5", "Value6", "MFlag6", "QFlag6", "SFlag6", "Value7",
"MFlag7", "QFlag7", "SFlag7", "Value8", "MFlag8", "QFlag8",
"SFlag8", "Value9", "MFlag9", "QFlag9", "SFlag9", "Value10",
"MFlag10", "QFlag10", "SFlag10", "Value11", "MFlag11", "QFlag11",
"SFlag11", "Value12", "MFlag12", "QFlag12", "SFlag12", "Value13",
"MFlag13", "QFlag13", "SFlag13", "Value14", "MFlag14", "QFlag14",
"SFlag14", "Value15", "MFlag15", "QFlag15", "SFlag15", "Value16",
"MFlag16", "QFlag16", "SFlag16", "Value17", "MFlag17", "QFlag17",
"SFlag17", "Value18", "MFlag18", "QFlag18", "SFlag18", "Value19",
"MFlag19", "QFlag19", "SFlag19", "Value20", "MFlag20", "QFlag20",
"SFlag20", "Value21", "MFlag21", "QFlag21", "SFlag21", "Value22",
"MFlag22", "QFlag22", "SFlag22", "Value23", "MFlag23", "QFlag23",
"SFlag23", "Value24", "MFlag24", "QFlag24", "SFlag24", "Value25",
"MFlag25", "QFlag25", "SFlag25", "Value26", "MFlag26", "QFlag26",
"SFlag26", "Value27", "MFlag27", "QFlag27", "SFlag27", "Value28",
"MFlag28", "QFlag28", "SFlag28", "Value29", "MFlag29", "QFlag29",
"SFlag29", "Value30", "MFlag30", "QFlag30", "SFlag30", "Value31",
"MFlag31", "QFlag31", "SFlag31")
Again, each of these are existing workspace objects I am trying to call as columns in a new data frame.
Edit:
Here is the code I used to create each of the vectors:
idCols <- list("ID","ID1","ID2","ID3","Year","Month","Element")
varCols <- paste(rep(list("Value","MFlag","QFlag","SFlag"),31),rep(c(1:31),each=4),sep="")
allCols <- c(idCols,varCols)
allList <- as.list(paste(allCols))
These objects are only temporarily in the workspace, as they are nested within a custom function. However, I would love to hear more about creating lists of objects.
Upvotes: 1
Views: 2004
Reputation: 57696
This avoids any coercion to/from a matrix:
data.frame(lapply(setNames(your_list, your_list), get))
Upvotes: 1
Reputation: 173677
This is sort of a shot in the dark:
x <- 1:5
y <- 1:5
> do.call(cbind,mget(c('x','y')))
x y
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 4 4
[5,] 5 5
Note that you need the characters in a vector not a list, so use c()
not list()
.
One weakness of this solution is that if each object is an atomic vector, the result will be a matrix. If all your data are numeric, this isn't a huge deal, you can simple coerce to a data frame using as.data.frame
. But if you have some character vectors mixed in there, you will end up with a character matrix, with all the numeric vectors converted to character.
I believe this shortcoming can be fixed by using cbind.data.frame
rather than just cbind
.
But of course, the fact that you have that many objects floating around in your workspace is a very bad sign. Typically in R we try not to organize our objects that way. Much better have lists of related objects. Then you can operate on them quickly and compactly as a group using tools like lapply
.
Upvotes: 3