Question: (Closed) Adding the csv file names as a separate column to the csv file
gravatar for paramount.amin
10 weeks ago by
paramount.amin0 wrote:


I have multiple CSV files (14000) in a folder and all of them have the same column heads (temperature, humidity, ..) and rows of my CSV files are five minutes time steps for all the year. I need to combine all the CSV files, using bind_rows to have one single CSV file. How can I add the file name as a column to my final file to be able to recognize which rows belong to what file's name? ( For example, the first file name is home1 and the second one is home 2 and I want to see which row belongs to home 1 and which row belongs to the second home?

R • 150 views
ADD COMMENTlink written 10 weeks ago by paramount.amin0

Use (edit) lapply to read and adding an id column to your files. Then do rbind on the files. This would give you what you want:

f <- list.files() # suppose you have your files in your current directory
file <- lapply(f, function(x){
  df <- read.csv(x) 
  df$id <- x 
fin_df <-"rbind", file)
ADD REPLYlink modified 10 weeks ago • written 10 weeks ago by Hamid Ghaedi740

Hello paramount.amin!

We believe that this post does not fit the main topic of this site.

Not a bioinformatics question.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.


ADD REPLYlink written 10 weeks ago by _r_am30k
Please log in to add an answer.
The thread is closed. No new answers may be added.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1567 users visited in the last hour