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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:50:17+00:00 2026-06-14T15:50:17+00:00

Possible Duplicate: When importing CSV into R how to generate column with name of

  • 0

Possible Duplicate:
When importing CSV into R how to generate column with name of the CSV?

I got a collection of files that I want to load into a single data frame. Each of the files I’m reading has the same structure, but different number of rows. Let’s say that each of those files represent a single participant, and I know I can read them using the code below:

files <- c("john.csv","fred.csv","nick.csv","alex.csv")
library(plyr)
dfoc <- ldply(files, read.csv, header = T)

Now, I want to be able to identify which rows belong to which participant. I want to add a single column for each of those files before I read them into a big data frame. The column I want to add will have a number of rows equal to number of rows for specific participant nrow(john). The column I add should simply contain an identifier, for example file name repeated nrow(x) times.

Any suggestions?

  • 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-14T15:50:18+00:00Added an answer on June 14, 2026 at 3:50 pm

    Here is what I’d do. (The key idea is to place the value of the id column and the just-read-in data.frame together inside a call to data.frame(). R’s recycling rules will make the id column have the right length in each case.)

    ## Set up a reproducible example
    a <- tempfile()
    b <- tempfile()
    write.csv(head(mtcars, 2), file=a)
    write.csv(tail(mtcars, 3), file=b)
    fnames <- c(a,b)
    
    ## Here's the code you are looking for
    do.call(rbind, lapply(fnames, function(X) {
        data.frame(id = basename(X), read.csv(X))})
    )
    #                 id             X  mpg cyl disp  hp drat    wt  qsec vs am gear carb
    # 1 file104862dd45aa     Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
    # 2 file104862dd45aa Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
    # 3  file1048d9e5764  Ferrari Dino 19.7   6  145 175 3.62 2.770 15.50  0  1    5    6
    # 4  file1048d9e5764 Maserati Bora 15.0   8  301 335 3.54 3.570 14.60  0  1    5    8
    # 5  file1048d9e5764    Volvo 142E 21.4   4  121 109 4.11 2.780 18.60  1  1    4    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Tool for importing CSV files into MySQL database? A guy at work
Possible Duplicate: DateTime.TryParse century control C# I have a C# app that's importing data
Possible Duplicate: Unload a module in Python After importing Numpy, lets say I want
Possible Duplicate: How to import LARGE sql files into mysql table Using php i
Is it possible to import csv data into mysql and automatically create the column
Possible Duplicate: Importing Python modules from different working directory I want to import a
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: Converting XML to JSON using Python? I am importing an XML feed

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.