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.jdobres– jdobres2018-04-10 11:55:09 +00:00Commented Apr 10, 2018 at 11:55
-
1Please put some data and some code that you have tried before and the error u get. Then we can try to help u.J0ki– J0ki2018-04-10 11:55:12 +00:00Commented Apr 10, 2018 at 11:55
Add a comment
|
1 Answer
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)
4 Comments
jogo
or
paste0(fn, ".csv")Carlos
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.
Luke Hayden
You're welcome. Using
Sys.glob("*.csv") will yield a list of all the .csv files in your directory.Carlos
Ive just found an easy way of extracting the file name
csvname<- basename(DataPathway)