0

I am working in R studio, where I have multiple lists of data( each list contains multiple observations in a single cell). As shown below: List data in R Studio

As shown above, the first column is the name of lists and third column Values are the observation in those lists(Each list with its associated values). What is required here is to convert these lists into data frame where all the names appear in one column and all the data appear in the second column. All data values have to be associated with each name with list names. How can I prepare a dataset with these requirements? i have taken out the name of lists through sapply function now the problem is how to extract the list values for those names?

Rahul Kumar
  • 39
  • 1
  • 11
  • 2
    Hi! Please give us a toy example of your data and what you expect, try `dpud(your_data)` or see here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – RLave Jul 11 '18 at 10:32
  • Provide example input and expected output, maybe something like: `data.table::rbindlist(lapply(myList, as.data.frame), idcol = "id")` – zx8754 Jul 11 '18 at 10:38
  • Another option is using `tibble::enframe(your_list)` for this – Nate Jul 11 '18 at 12:06
  • It worked as i was expecting, Thank you very much(zx8754) – Rahul Kumar Jul 23 '18 at 16:29

1 Answers1

0

Using DF defined reproducibly in the Note at the end use separate_rows in tidyr:

library(dplyr)
library(tidyr)

DF %>% 
   select(Alltrain, Value) %>% 
   separate_rows(Value, convert = TRUE)

giving:

  Alltrain Value
1        A 1e+02
2        A 1e+03
3        B 1e+07

Note

DF <- data.frame(Alltrain = c("A", "B"), Type = NA, Value = c("1e2 1e3", "1e7"),
 stringsAsFactors = FALSE)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341