this is my first question on this forum so I hope I follow all the right procedures.
I was wondering if it's possible to create a list in R from a set of variables with distinct names and values in one line. Specifically, I already have a list list_cond <- list("foo1", "foo2", "foo3",...)
and a list of lists list_vals <- list(list(1, 2, 3), list(4, 5, 6), list(7, 8, 9))
and would like to create a list new_list <- list(f("foo1")=2, f("foo2")=5, f("foo3")=8)
procedurally where a string modifying function f
is applied to each element of list_cond
before it is used as a name for the corresponding element which is indexed from list_vals
. Is there a way to do this in a single line?
I found sapply
as a method in this thread by using USE.NAMES=TRUE
but it seems to only generate the names from the function passed in, and the values are just the same as the names. This thread is also related, but I specifically want to generate both the names and the values in the same line, not just add names to the values.
If doing this in one line isn't possible, are there any specific methods that are efficient as opposed to just creating the list and using some apply
function to add the names later?
EDIT: as this will be running on a specific server which doesn't have a lot of R packages, so built in functions would be preferable