Reputation: 2493
I managed to import 10 .csv
files by making use this elegant lapply
solution.
The output now is a list
of 10 data.frames
.
Each data.frame contains the variables year
and value
:
file_names <- dir("../XYZ", pattern = glob2rx("*.csv"))
data_list <- lapply(filenames, read.table, header = TRUE)
head(data_list[[1]])
year value
1 200712 2320,00
2 200712 120,00
3 200712 188,00
4 200712 1328,00
5 200712 46,00
6 200712 98,00
head(data_list[[2]])
year value
1 200812 320,00
2 200812 1120,00
3 200812 1288,00
4 200812 1128,00
5 200812 746,00
6 200812 938,00
Unfortunately, the variable year
is formatted yyyymm
, but I want it to be yyyy
.
Hence I would like to use another lapply
or Map
to change the format of year
in every single data.frame. Something like:
data_list <- lapply(data_list[["year"]], FUN = substr(1,4), ... )
or:
data_list <- Map(substr(1,4), data_list, "year")
But that doesn't work...Any ideas?
Upvotes: 1
Views: 64
Reputation: 887048
You could try
data_list <- lapply(data_list, function(x) {x$year <- substr(x$year, 1,4)
x})
Upvotes: 1