Answers.Body: <p>I don't know whether this will actually work since you didn't provide a reproducible example...</p>
  <pre><code>gene.sets = lapply(1:nrow(h), function(x) unname(unlist(h[x,-1])))
  names(gene.sets) = h[,1]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-21T18:36:14.007Z
Answers.Text: Thanks worked. As different pathways have different number of genes involved to I got a lot of empty thing. The pathway with the most number of genes has 292 genes so the code assumes each pathway should have 292 genes that is why for the rest puts "". I have added the result of your code in my main post
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-21T19:38:57.990Z
Answers.Text: Thanks again I have added solution to remove ""
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-21T18:09:46.767Z
Answers.LastActivityDate: 2023-06-21T18:09:46.767Z
Answers.UserDisplayName: user438383
Body: <p>I have data frame each row is a pathway and it involved genes like</p>
  <p><a href="https://i.sstatic.net/Z7BC5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Z7BC5.png" alt="enter image description here" /></a></p>
  <p>like</p>
  <pre><code>&gt; head(h)
      Release.of.Cancer.Cell.Antigens.1  ARID1A    ATM  BIRC3   BLM BNIP3   BRCA1   BRCA2 BRD3  BRD4 BRIP1 CCNA1 CCND1
  V1        Cancer Antigen Presentation    ARG2   ATF3    B2M BATF3   BLK   CCL22 CCL3/L1 CCL4  CCL5  CCR5  CD14  CD19
  V2      T-cell Priming and Activation ADORA2A  BATF3   BTLA  CCL2 CCL22 CCL3/L1    CCL4 CCL5  CCR2  CCR5  CD1C   CD2
  V3 Immune Cell Localization to Tumors     A2M ADGRE1 ANGPT2  APOE  ATF3     AXL   BATF3  BLK CCL13 CCL14 CCL18 CCL19
  </code></pre>
  <p>I want to convert that like</p>
  <pre><code>gene.sets &lt;- list(Release.of.Cancer.Cell.Antigens = c(&quot;ARID1A&quot;, &quot;ATM&quot;,  &quot;BIRC3&quot;,    &quot;BLM&quot;),
              Cancer_Antigen_Presentation = c(&quot;ARG2&quot;,&quot;ATF3&quot;,&quot;B2M&quot;))
  </code></pre>
  <p>I have not done anything so far as I am not certain what exactly I should google for.</p>
  <p>By code the user kindly provided I get this</p>
  <pre><code>gene.sets = lapply(1:nrow(h), function(x) unname(unlist(h[x,-1])))
  names(gene.sets) = h[,1]
  
  [91] &quot;IGF2R&quot;     &quot;IHH&quot;       &quot;IL11&quot;      &quot;IL11RA&quot;    &quot;IL12RB2&quot;   &quot;IL15&quot;      &quot;IL17A&quot;     &quot;IL1A&quot;      &quot;IL1B&quot;     
  [100] &quot;IL22RA1&quot;   &quot;IL2RA&quot;     &quot;IL2RB&quot;     &quot;IL6&quot;       &quot;IL7R&quot;      &quot;IRF1&quot;      &quot;IRF2&quot;      &quot;IRF3&quot;      &quot;IRF4&quot;     
  [109] &quot;IRF7&quot;      &quot;IRF9&quot;      &quot;ISG15&quot;     &quot;ITGAE&quot;     &quot;JAK1&quot;      &quot;JAK2&quot;      &quot;JAK3&quot;      &quot;KIT&quot;       &quot;LAG3&quot;     
  [118] &quot;LAIR1&quot;     &quot;LIF&quot;       &quot;LTB&quot;       &quot;MX1&quot;       &quot;NCAM1&quot;     &quot;NKG7&quot;      &quot;OAS1&quot;      &quot;OAS2&quot;      &quot;OAS3&quot;     
  [127] &quot;PDCD1&quot;     &quot;PDCD1LG2&quot;  &quot;PF4&quot;       &quot;PIK3CA&quot;    &quot;PIK3CG&quot;    &quot;PIK3R5&quot;    &quot;PRF1&quot;      &quot;PRLR&quot;      &quot;PROM1&quot;    
  [136] &quot;PSMB10&quot;    &quot;PSMB5&quot;     &quot;PSMB9&quot;     &quot;PTEN&quot;      &quot;REN&quot;       &quot;RORC&quot;      &quot;RSAD2&quot;     &quot;SELL&quot;      &quot;SELP&quot;     
  [145] &quot;SIRPA&quot;     &quot;SOX11&quot;     &quot;SOX2&quot;      &quot;SPRY4&quot;     &quot;STAT1&quot;     &quot;STAT2&quot;     &quot;STAT4&quot;     &quot;TBX21&quot;     &quot;TIGIT&quot;    
  [154] &quot;TNF&quot;       &quot;TNFAIP3&quot;   &quot;TNFRSF10B&quot; &quot;TNFRSF10C&quot; &quot;TNFRSF11A&quot; &quot;TNFRSF11B&quot; &quot;TNFRSF14&quot;  &quot;TNFRSF17&quot;  &quot;TNFRSF18&quot; 
  [163] &quot;TNFRSF1A&quot;  &quot;TNFRSF1B&quot;  &quot;TNFRSF4&quot;   &quot;TNFRSF8&quot;   &quot;TNFRSF9&quot;   &quot;TNFSF10&quot;   &quot;TNFSF12&quot;   &quot;TNFSF13&quot;   &quot;TNFSF13B&quot; 
  [172] &quot;TNFSF18&quot;   &quot;TNFSF4&quot;    &quot;TNFSF8&quot;    &quot;TPSAB1/B2&quot; &quot;TRAF1&quot;     &quot;TSLP&quot;      &quot;VEGFA&quot;     &quot;WNT7B&quot;     &quot;&quot;         
  [208] &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;         
  [217] &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;         
  [226] &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;         
  [235] &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;         
  [244] &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;         
  [253] &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;          &quot;&quot;    
  
  x=lapply(gene.sets, function(z){ z[!is.na(z) &amp; z != &quot;&quot;]})
  </code></pre>
  
Tags: <r>
Title: Converting a data frame to multiple lists
ThreadText: # Converting a data frame to multiple lists
  
  (Asked by: username_0 on 2023-06-21)
  
  I have data frame each row is a pathway and it involved genes like
  
  [![enter image description here](https://i.sstatic.net/Z7BC5.png)](https://i.sstatic.net/Z7BC5.png)
  
  like
  
  ```
  > head(h)
      Release.of.Cancer.Cell.Antigens.1  ARID1A    ATM  BIRC3   BLM BNIP3   BRCA1   BRCA2 BRD3  BRD4 BRIP1 CCNA1 CCND1
  V1        Cancer Antigen Presentation    ARG2   ATF3    B2M BATF3   BLK   CCL22 CCL3/L1 CCL4  CCL5  CCR5  CD14  CD19
  V2      T-cell Priming and Activation ADORA2A  BATF3   BTLA  CCL2 CCL22 CCL3/L1    CCL4 CCL5  CCR2  CCR5  CD1C   CD2
  V3 Immune Cell Localization to Tumors     A2M ADGRE1 ANGPT2  APOE  ATF3     AXL   BATF3  BLK CCL13 CCL14 CCL18 CCL19
  
  ```
  
  I want to convert that like
  
  ```
  gene.sets <- list(Release.of.Cancer.Cell.Antigens = c("ARID1A", "ATM",  "BIRC3",    "BLM"),
              Cancer_Antigen_Presentation = c("ARG2","ATF3","B2M"))
  
  ```
  
  I have not done anything so far as I am not certain what exactly I should google for.
  
  By code the user kindly provided I get this
  
  ```
  gene.sets = lapply(1:nrow(h), function(x) unname(unlist(h[x,-1])))
  names(gene.sets) = h[,1]
  
  [91] "IGF2R"     "IHH"       "IL11"      "IL11RA"    "IL12RB2"   "IL15"      "IL17A"     "IL1A"      "IL1B"     
  [100] "IL22RA1"   "IL2RA"     "IL2RB"     "IL6"       "IL7R"      "IRF1"      "IRF2"      "IRF3"      "IRF4"     
  [109] "IRF7"      "IRF9"      "ISG15"     "ITGAE"     "JAK1"      "JAK2"      "JAK3"      "KIT"       "LAG3"     
  [118] "LAIR1"     "LIF"       "LTB"       "MX1"       "NCAM1"     "NKG7"      "OAS1"      "OAS2"      "OAS3"     
  [127] "PDCD1"     "PDCD1LG2"  "PF4"       "PIK3CA"    "PIK3CG"    "PIK3R5"    "PRF1"      "PRLR"      "PROM1"    
  [136] "PSMB10"    "PSMB5"     "PSMB9"     "PTEN"      "REN"       "RORC"      "RSAD2"     "SELL"      "SELP"     
  [145] "SIRPA"     "SOX11"     "SOX2"      "SPRY4"     "STAT1"     "STAT2"     "STAT4"     "TBX21"     "TIGIT"    
  [154] "TNF"       "TNFAIP3"   "TNFRSF10B" "TNFRSF10C" "TNFRSF11A" "TNFRSF11B" "TNFRSF14"  "TNFRSF17"  "TNFRSF18" 
  [163] "TNFRSF1A"  "TNFRSF1B"  "TNFRSF4"   "TNFRSF8"   "TNFRSF9"   "TNFSF10"   "TNFSF12"   "TNFSF13"   "TNFSF13B" 
  [172] "TNFSF18"   "TNFSF4"    "TNFSF8"    "TPSAB1/B2" "TRAF1"     "TSLP"      "VEGFA"     "WNT7B"     ""         
  [208] ""          ""          ""          ""          ""          ""          ""          ""          ""         
  [217] ""          ""          ""          ""          ""          ""          ""          ""          ""         
  [226] ""          ""          ""          ""          ""          ""          ""          ""          ""         
  [235] ""          ""          ""          ""          ""          ""          ""          ""          ""         
  [244] ""          ""          ""          ""          ""          ""          ""          ""          ""         
  [253] ""          ""          ""          ""          ""          ""          ""          ""          ""    
  
  x=lapply(gene.sets, function(z){ z[!is.na(z) & z != ""]})
  
  ```
  
  **Tags:** r
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-06-21. Score: 2 | Accepted answer
  
  I don't know whether this will actually work since you didn't provide a reproducible example...
  
  ```
  gene.sets = lapply(1:nrow(h), function(x) unname(unlist(h[x,-1])))
  names(gene.sets) = h[,1]
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks worked. As different pathways have different number of genes involved to I got a lot of empty thing. The pathway with the most number of genes has 292 genes so the code assumes each pathway should have 292 genes that is why for the rest puts "". I have added the result of your code in my main post  
  > **username_0**: Thanks again I have added solution to remove ""  
  
  ---
  
  