-1

I have a list named mylist like:

mylist = list(
  a = c("35","45","56",..., "67", "66", "89"),
  b = c("67"),
  c = c("55")
)

$a
[1] "35" "45" "56" ...
[19] "67" "66" "89"

$b
[1] "67"

$c
[1] "55"

And I would like to add at the beginning of each list rows the list names. To be like this:

$a
[1] "a" "35" "45" "56" ... "67" "66" "89"

$b
[1] "b" "67"

$c
[1] "c" "55"

How can I do that? Thanks!

I could not find a solution yet.

I edited the list how it should be. Actually, my aim to export this later on as a text file in which each row starts with list names. So I want the output to be like:

a 35 45 56 ... 67 66 89
b 67
c 55

recnid
  • 3
  • 2
  • Could you edit your question to include reproducible data? Particularly with this question, is it important to have a more exact data structure. If you're not sure how to do this, [see here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for some tips. Good luck! – jpsmith Jul 21 '23 at 16:31
  • 2
    why is `a` added twice in the vector? at the beginning and in the middle? anyway you could do `Map(c, names(mylist), mylist)` – Onyambu Jul 21 '23 at 16:35
  • It seems that you reset `options(width = xx)` so that the 4th element turns to the next line. – Darren Tsai Jul 21 '23 at 16:40
  • 1
    `mylist$a` is just a single vector. The fact that it appears as 2 rows of 3 elements is just an artifact of the way you printed it. Please clarify if you want what your question asks for, that is: put "a" at the start of the list element, or what you show: split `mylist$a` into multiple vectors each starting with "a"? – divibisan Jul 21 '23 at 16:43
  • So, that's a completely different question. You're asking "how to print a list to a file, printing the name of the current list item at the start of each line". Searching for that in google gives me this question: [Write list to a text file, preserving names, R](https://stackoverflow.com/q/8261590/8366499) which you might have to modify if you want to only list N-items on each line, instead of the whole list element – this question might be helpful for that [In R, How to write from a list to file with a set amount of elements on each line?](https://stackoverflow.com/q/66535035/8366499) – divibisan Jul 21 '23 at 18:14
  • Thanks everyone for the help. – recnid Jul 21 '23 at 18:23

1 Answers1

0

Your list structure doesn't make complete sense to me for "a". It looks like a single vector yet you want the name ("a") added in more than one place...

In lieu of a reprex, here's an option to add the list name at the beginning using purrr.

library(purrr)

tmp = list(
  a = c("35","45","56", "67", "66", "89"),
  b = c("67"),
  c = c("55")
)

tmp = imap(tmp, ~ c(.y, .x))

$a
[1] "a"  "35" "45" "56" "67" "66" "89"

$b
[1] "b"  "67"

$c
[1] "c"  "55"

OPs previous comment mentioned exporting the modified list but the comment has been edited. Leaving the code below incase that's something they still want done.

Export the above list you can run the following.

lapply(tmp, write, "tmp.txt", append = T, ncolumns = 7)

Example output of "tmp.txt" enter image description here

Jamie
  • 1,793
  • 6
  • 16
  • Hi, thank you for your answer. – recnid Jul 21 '23 at 16:44
  • Please see my modified answer for outputting to a text fie. All you need to do is change `ncolumns = N` to a number that is the same as the length of your longest list item. – Jamie Jul 21 '23 at 16:53
  • Hi Jamie, it is very close to what I want, but still I am missing the names of list at the beginning of each line. Do you know how to add names to the beginning of each line? – recnid Jul 21 '23 at 17:00
  • The code above works for me. I see you edited your initial list, however it is not possible to include `...` in a list that way. Please provide your actual list without the "..." – Jamie Jul 21 '23 at 17:07
  • It is working for me too, sorry. Thanks very much! – recnid Jul 21 '23 at 18:06