Carlos
Carlos

Reputation: 47

Inserting the file name as column value in a csv file in R

I have a file called “Day1.csv” with X number or rows. I would like to create a new column that includes the file name (“Day1”) in each row (from 1 to X). I have seen similar posts but I cannot get it to work. I was wondering is someone could give me a hand with this. Many thanks in advance.

Upvotes: 0

Views: 208

Answers (1)

Luke Hayden
Luke Hayden

Reputation: 712

Try this:

fn <- "Day1"
df <- read.csv(file=paste(fn, ".csv", sep=""))
df$filename <- fn

This way you can reproducibly fetch other filenames if you need (Day2, Day3, etc)

Upvotes: 1

Related Questions