I have a vector of city names called cities
:
> cities
[1] 'amsterdam''atlanta''bangalore'
[4] 'bogota''boston''brisbane'
[7] 'brussels''cairo''cape'
I have two folders where the same cities are present and ordered in the same way. I have turned each folder into a list. Each file in list1
contains geographic data for one city. Each file in list2
contains travel time data for one city.
> list1
[[1]]
[1] "C:/geo/amsterdam.json"
[[2]]
[1] "C:/geo/atlanta.json"
[[3]]
[1] "C:/geo/bangalore.json"> list2
[[1]]
[1] "C:/time/amsterdam.csv"
[[2]]
[1] "C:/time/atlanta.csv"
[[3]]
[1] "C:/time/bangalore.csv"
I would like to write a loop that,
- Based on the position of the city in
cities
, - reads in the corresponding file from
list1
, - reads in the corresponding file from
list2
, - merges the two datasets together into a dataset whose name corresponds to the index position in
cities
Something like that:
for (i in length(cities)){
geo <- (sf::st_read(list1[i])
time <- (data.table::fread(list2[i])
merged[i] <- merge(geo, time, by='id') #both datasets have an 'id' column
}
Currently, this loop gives the error number of items to replace is not a multiple of replacement length
.
When I try indexing each output (i.e. geo[i] <- (sf::st_read(list1[i])
...), I get the error new columns would leave holes after existing columns
.
Is there a way to carry out operations on elements of a list based on the position of these elements (or their equivalent) in another list/vector?