2

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

  • Do you need `map(list_vals, ~ set_names(.x, unlist(list_cond)))` – akrun Jul 13 '18 at 15:41
  • I wasn't able to test it as I am running R on a server without many libraries. I updated my post to put this detail in. Is there a way to do this with built in R functions? –  Jul 13 '18 at 16:00
  • Based on the expected output, are you extracting only the second element – akrun Jul 13 '18 at 16:04
  • If that is the case `map(list_vals, pluck, 2) %>% set_names(paste0(unlist(list_cond), "_", seq_along(list_vals)))` Or with `base R` `setNames(lapply(list_vals, '[[', 2), paste0(unlist(list_cond), "_", seq_along(list_vals)))` – akrun Jul 13 '18 at 16:07
  • Your *new_list* names seems counter intuitive. Why `1_1` suffix patterns for the second element in each of the nested elements? And is the third name missing a number for *foo3_3*? – Parfait Jul 13 '18 at 16:09
  • I am getting an error: `could not find function %>%` –  Jul 13 '18 at 16:12
  • My bad, I wrote my example badly and will update my post. I just meant that I'm applying some function on to each string. In reality my program is actually reading values from a tkentry textvariable using `tclvalue` but I wanted to make it simpler. –  Jul 13 '18 at 16:12
  • @akrun it worked! that's what I'm looking for, thanks! –  Jul 13 '18 at 16:18

1 Answers1

0

We extract the second element of the list and then set the names of the list with 'list_cond' (is a list so unlist it to create a vector)

setNames(lapply(list_vals, '[[', 2), 
          paste0(unlist(list_cond), "_", seq_along(list_vals)))

A tidyverse approach would be

library(tidyverse)
map(list_vals, pluck, 2) %>% 
     set_names(paste0(unlist(list_cond), "_", seq_along(list_vals)))

data

list_cond <- list("foo1", "foo2", "foo3")
list_vals <- list(list(1, 2, 3), list(4, 5, 6), list(7, 8, 9))
akrun
  • 874,273
  • 37
  • 540
  • 662