Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:06:21+00:00 2026-05-27T02:06:21+00:00

I want to write a list to a text file, preserving the names. This

  • 0

I want to write a list to a text file, preserving the names.

This is similar to R: Print list to a text file but with names which I want to print out also, at the start of each line:

> print(head(mylist,2))
$first
[1] 234984  10354  41175 932711 426928
$second
[1] 1693237   13462

mylist.txt
first   234984  10354  41175 932711 426928
second  1693237   13462

Any ideas?

Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:06:21+00:00Added an answer on May 27, 2026 at 2:06 am

    The cat function will print to a device (console by default) and not add any of the usual annotations, but it cannot accept a list as an argument, so everything needs to be an atomic vector. The deparse( substitute()) gambit is the way to recover names of lists that were passed to a function. Just using names(x) inside the function fails to recover the name of the original argument.

     mylist <- list(first =c( 234984,  10354,  41175, 932711, 426928), 
                    second =c(1693237, 13462))
     fnlist <- function(x){ z <- deparse(substitute(x))
                             cat(z, "\n")
                             nams=names(x) 
                       for (i in seq_along(x) ) cat(nams[i],  x[[i]], "\n")}
     fnlist(mylist)
    mylist 
    second 234984 10354 41175 932711 426928 
    first 1693237 13462 
    

    This version would output a file (and you could substitute “\t” if you wanted tabs between names and values

    fnlist <- function(x, fil){ z <- deparse(substitute(x))
                             cat(z, "\n", file=fil)
                             nams=names(x) 
                       for (i in seq_along(x) ){ cat(nams[i], "\t",  x[[i]], "\n", 
                                                file=fil, append=TRUE) }
                             }
     fnlist(mylist, "test")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a following script: given a text file with the list
I want to write the list of file names from a Given directory path
I want to write a structure which has a list of integer id. The
I want to write a mysql statement which will return a list of results
I want to write this program to find a keyword in a list. If
I would like to write an address in a text file, which can be
I want to read a list of integers from a text file, I just
I have a list of words in a text file. What I want is
i'm supposed to write code which when given a text file (source code) as
I want to write a list of windows commands(it's a long list) where it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.