I would like to import multiple CSV files using dplr into one tibble, but would also like to capture the file name of each file as a new field as part of the data import.
Let's say file01.txt has this data
F1, F2
A, 1
B, 2
C, 3
and file02.txt has this data
F1, F2
D, 4
E, 5
F, 6
and file03.txt has this data
F1, F2
H, 7
I, 8
J, 9
I can make a vector with the file names:
Fnms <- c("file01.txt", "file02.txt", "file03.txt" )
... and read the first file and add the first file name in a new field.
tmp <- read_csv(Fnms[1])
tmp$Fnm <- Fnms[1]
To read the other two files and append their names I tried a loop structure and messy if-else statements:
for (i in 2:3){
tmp <- tmp %>%
bind_rows(read_csv(Fnms[i])) %>%
mutate(Fnm = ifelse(row_number() > 3 & row_number() <= 6, Fnms[i], Fnm )) %>%
mutate(Fnm = ifelse(row_number() > 6 , Fnms[i], Fnm ))
}
Which produces this incorrect result (also not sure why the first mutate is not skipped when i=3?)
> tmp
# A tibble: 9 × 3
F1 F2 Fnm
<chr> <dbl> <chr>
1 A 1 File01.txt
2 B 2 File01.txt
3 C 3 File01.txt
4 D 4 File03.txt
5 E 5 File03.txt
6 F 6 File03.txt
7 G 7 File03.txt
8 H 8 File03.txt
9 I 9 File03.txt
I have a solution that works in base R using a loop and rbind, but I'm keen to find a dplyr solution as I'm still learning this newer version of the R language!