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 8894385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:28:58+00:00 2026-06-14T23:28:58+00:00

I have a problem processing filename as variable in a loop in R files

  • 0

I have a problem processing filename as variable in a loop in R

files <- list.files(pattern = "*.tab",full.name=T)
for (a in files) { aname <- strsplit(basename(a), "\\.")[[1]][1]
                   aname <- read.table(a,header=TRUE, sep="\t",comment.char="")
                }

It produces only one object: aname, if I use following:

for (a in files) { c(strsplit(basename(a), "\\.")[[1]][1]) <- read.table(a,header=TRUE,
                        sep="\t",comment.char="")
                }

it produces: could not find function “c<-“. But if I do

for (a in files) { aname <- strsplit(basename(a), "\\.")[[1]][1]
                   print(aname)
                }

The output is a list of files without extension, as expected. So, the question is: how do I make result of a function a variable name?
Thank you!

  • 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-06-14T23:28:59+00:00Added an answer on June 14, 2026 at 11:28 pm

    The problem is not in processing the file name but in the way you’ve written the loop. Essentially you are doing:

    for(i in list.of.files) {
      foo <- processName(i)
      foo <- read.table(foo)
    }
    

    when viewed like that, it is clear that foo gets written to twice for each iteration of the loop and hence can only ever take the value of the last read.table() call.

    What you want to do is allocate storage for the list of files before you enter the loop, and then fill in that list as you go. For example:

    aname <- vector("list", length = length(files))
    fnames <- character(length(files))
    for(i in seq_along(aname)) {
      fnames[i] <- strsplit(basename(files[i]), "\\.")[[1]][1]
      aname[i] <- read.table(a, header=TRUE, sep="\t", comment.char="")
    }
    names(aname) <- fnames
    

    Having the objects contained in a list is a useful feature so you don;t have all those objects banging around. As they are contained within a list you can operate on each object using lapply() or similar.

    If you really want to have an individual object that is has the filename without extension as its name for all files, then you could use assign(), but I don’t recommend it

    files <- list.files(pattern = "*.tab", full.name=TRUE)
    for (a in files) {
      aname <- strsplit(basename(a), "\\.")[[1]][1]
      assign(aname, read.table(a, header=TRUE, sep="\t", comment.char="")
    }
    

    See ?assign for more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files: metadata.csv: contains an ID, followed by vendor name, a filename,
I have problem on my blog: http://osify.com while processing add new post with above
I have recently created a HTML5 canvas animation (also using Processing.js). The problem is
I have problem on processing input request parameter (of course it's type String) to
I have a problem with processing a SQL Server database dump in PHP. I
i have a problem about partial processing. (I use primefaces 3.4.1.) I have a
I have a problem regarding an actual project using Java, Kinect (OpenNI) and Processing.
I have a problem with forcing download of some mp3/zip files. The problem is
I have no problems processing jQuery GET requests in a controller, however I cannot
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar

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.