How to convert a list to a dataframe that remains two columns, given the duplicity in the datasets?

I have a list in R, as in the following example:

> exemplo
$`c("64735", "254528")`
[1] "703322"

$`100288287`
[1] "100426225"

$`391195`
[1] "707174" "706331"

I am trying to convert this list to a dataframe while remaining two columns. I used this function:

> Reduce(rbind, Map(cbind, exemplo, gene = names(exemplo)))
                 gene                      
[1,] "703322"    "c(\"64735\", \"254528\")"
[2,] "100426225" "100288287"               
[3,] "707174"    "391195"                  
[4,] "706331"    "391195"

but I still have a problem in the second column. Any idea how to do this.

+4
source share
1 answer

We are an stack“example” listin 2 columns data.frame, convert it to data.table( setDT(..), grouped by “values”, split the “ind” column into characters other than a number unlist, and delete the lines in which the output ('V1') is empty or empty.

library(data.table)
setDT(stack(exemplo))[, unlist(strsplit(as.character(ind), 
         '[^0-9]+')), by = values][V1!='']
#      values        V1
#1:    703322     64735
#2:    703322    254528
#3: 100426225 100288287
#4:    707174    391195
#5:    706331    391195
+4
source

Source: https://habr.com/ru/post/1621997/


All Articles