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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:47:16+00:00 2026-06-17T08:47:16+00:00

trying to create a function that looks up a bunch of CSV files in

  • 0

trying to create a function that looks up a bunch of CSV files in a directory and then, taking the file ID as an argument, outputs a table (actually data frame – new to R language) where there are 2 columns, one titled ID for the corresponding id parameter and the second column will be the count of rows in that file.

The files are all titled 001.csv – 322.csv

e.g. output would look like column title: ID, first record: 001 (derived from 001.csv), second column: title "count of rows", first record

The function looks like so: myfunction(directory,id)
Directory is the folder where the csv files are and id can be a number (or vector?) e.g. simply 1 or 9 or 100 or it can be a vector like so 200:300.

In the case of the later, 200:300, the output would be a table with 100 rows where the 1st row would be 200 with say 10 rows of data within it.

So far:

complete <- function(directory,id = 1:332) {

    # create an object to help read the appropriate csv files later int he function
    
     csvfilespath <- sprintf("/Users/gcameron/Desktop/%s/%03d.csv", directory, id)

     colID <- sprintf('%03d', id)

    # now, how do I tell R to create a table with 2 columns titled ID and countrows?
    # Now, how would I take each instance of an ID and add to this table the id and         count of rows in each?
}

I apologize if this seems really basic. The tutorial I’m on moves fast and I have watched each video lecture and done a fair amount of research too.
SO is by far my favourite resource and I learn better by using it. Perhaps because it’s personalised and directly applicable to my immediate tasks. I hope my questions also benefit others who are learning R.

BASED ON FEEDBACK BELOW

I now have the following script:

complete <- function(directory,id = 1:332) {
    csvfiles <- sprintf("/Users/gcameron/Desktop/%s/%03d.csv", directory, id)
    nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))
    data.frame(ID=id, countrows=sapply(csvfiles,function(x) length(count.fields(x)))
    }

Does this look like I’m on the right track?
I’m receiving an error "Error: unexpected ‘}’ in:
"data.frame(ID=id, countrows=sapply(csvfiles,function(x) length(count.fields(x)))
}"

I cannot see hwere the extra "}" is coming from?

  • 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-17T08:47:16+00:00Added an answer on June 17, 2026 at 8:47 am
    data.frame(ID=id, countrows=sapply(csvfilepath, function(x) length(count.fields(x))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom function that unbinds and then binds an event.
I'm trying to create a function that would add entries to a json file.
I'm trying to create a function that would append two vectors together, but I
I am trying to create a function that updates another value, and initially (after
I'm trying to create a function that searches up a WebControl's parent-child relationship (basically
I am trying to create a function that will block access to some of
I'm trying to create a function that would dynamically allocate an array, sets the
I am trying to create a function that given a divid, and a list
I'm trying to create a function that will simply allow me to pass an
I am trying to create a function that sends 100 call per second, and

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.