0

I have a list that look like this:

setlist2 <- list(wsb_b6, wsb_id8)
[[1]]
[1] "Gm10116"       "Tpm3-rs7"      "Wdfy1"         "Rps3a2"        "AK157302"      "Gm6563"        
"Gm9825"        "Gm10259"       "Gm6768" 
[[2]]
 [1] "Gm6401"        "Ecel1"         "Hpca"          "Tmem176a"      "Lepr"          
"Baiap3"        "Fam183b"       "Vsx2"          "Vtn"          

I need it to look like this:

$wsb_b6
    [1] "Gm10116"       "Tpm3-rs7"      "Wdfy1"         "Rps3a2"        "AK157302"      "Gm6563"        
    "Gm9825"        "Gm10259"       "Gm6768" 
$wsb_id8
    [1] "Gm6401"        "Ecel1"         "Hpca"          "Tmem176a"      "Lepr"          
"Baiap3"        "Fam183b"       "Vsx2"          "Vtn"

I know that by doing it manually I can achieve it but it is more that 100 each, there's got to be a better way

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Dan
  • 45
  • 7
  • Do you want to replace the values in 2nd list with that of 1st list but then both the list would have same values? – Ronak Shah Feb 27 '21 at 03:04
  • Hello, the content of the lists are both fine. the problem is that instead of double brackets with numbers [[1]] and [[2]], like in the code I did, I need to have $name1 and $name2. I just don't know how to do that. I hope I made myself clearer. Thanks – Dan Feb 27 '21 at 03:11

3 Answers3

2

#I found that I had to unlist my two previous lists

wsb_b6 <-wsb_b6[,1]
wsb_b6 <-unlist(wsb_b6)

wsb_id8 <-wsb_id8[,1]
wsb_id8 <- unlist(wsb_id8)

#And then list them again, but like this

   setlist2 <-list(wsb_b6=wsb_b6, wsb_id8= wsb_id8)
Dan
  • 45
  • 7
1

Use dplyr::lst

setlist2 <- dplyr::lst(wsb_b6, wsb_id8)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

It sounds like you want to create a named list. Specifically, you want to create a named list where the names are taken from the names of the variables in the environment.

This is similar to this question: Can lists be created that name themselves based on input object names?

I don't believe there is a simple function to do this in base R, but you can using the function llist from the package Hmisc:

library(Hmisc)

setlist2 <- llist(wsb_b6, wsb_id8)
DMR
  • 1,479
  • 1
  • 8
  • 11