I have 5 data files and each file name is xxx2014.dat, xxx2015.dat...xxx2018.dat. I merge these files together.
Now, I want to append each year back in my new merge dataset, how can I do it in r?
I try to use append() but I have no idea how r can know which row are 2014 or 2015...
yearslist = c("2014","2015","2016","2017","2018")
for (year in yearslist) {
filename = paste0("xxx", year, ".dat")
dataname <- assign(
paste0("xxx", year),
read_fwf(
file = filename,
fwf_positions(...),
col_names = c("relationship","age","race","sex")
)
)
}
newmergedata <- bind_rows(fileA,fileB)
I expected to get my new merge dataset have one column is corresponding years.
For example:
Year Sex Region
2014 001 1
2014 002 1
2015 001 2
2018 002 1
However, now I only have
Sex Region
001 1
002 1
001 2
002 1
How to find the value their corresponding year in new merge data?