0

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.

2
  • Please provide a reproducible example of what you have tried so far. Commented Apr 10, 2018 at 11:55
  • 1
    Please put some data and some code that you have tried before and the error u get. Then we can try to help u. Commented Apr 10, 2018 at 11:55

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

or paste0(fn, ".csv")
Thank you Luke. I have many files to go through so I was wondering if the name of the file can be automatically extracted so I dont have to type it each time.
You're welcome. Using Sys.glob("*.csv") will yield a list of all the .csv files in your directory.
Ive just found an easy way of extracting the file name csvname<- basename(DataPathway)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.