0

I have a list with five different elements inside. e.g.

my_list                           
# $obj1
# [1] 1 2 3 4 5
# 
# $obj2
# [1] "XYZ"
# 
# $obj3
# [1] "g" "f" "e" "d"

I would like to create a let's say txt file that will print ALL values in one column, and the column next to it will be the EXACT NAME of the element/group that it belonged to. e.g.

excel sheet example

I've tried some methods like un-listing, but no luck getting exactly what I want. Would appreciate any help!

  • `df <- stack(my_list)` would give you the dataframe. You can then use `write.csv` or `writexl::write_xlsx` to write it to excel. – Ronak Shah Sep 03 '21 at 07:14
  • thanks! I tried it. It didn't work on my list and it gave this error: – Error in data.frame(values = unlist(unname(x)), ind, stringsAsFactors = FALSE) : arguments imply differing number of rows: 337, 6, but it works when I stack each element of the list separately e.g. stack(my_list[1]) and then I ended up concatenating them all together. – Erfan Shekarriz Sep 03 '21 at 07:21
  • I can't reproduce this on the data that you have shared. I used `my_list <- list(obj1 = 1:5, obj2 = 'XYZ', obj3 = c('g', 'f', 'e', 'd'))` and using `stack(my_list)` gives a 2-column dataframe similar to the expected output shown. Maybe in your actual data you have a different structure than what is shown here in the example. – Ronak Shah Sep 03 '21 at 07:25

0 Answers0