R: Loop outputting only the last iteration

廉价感情. 提交于 2021-02-11 17:03:28

问题


I have 39 json files stored in a folder. They all have three columns in common: MOVEMENT_ID, DISPLAY_NAME and geometry. I would like to arrive at a dataset containing the data from all files, organised into these columns. I am using a for loop to do so.

path = "~/geoboundaries" #path to the folder where the files are stored
file.names=as.list(dir(path, pattern='.json', full.names=T)) 
#make a list of all file names
out.file=st_sf(MOVEMENT_ID=factor(), DISPLAY_NAME=factor(),
 geometry=st_sfc(), crs=st_crs(4326), sf_column_name='geometry') 
#define a void sf object in which the loop results will be stored

I want to make a loop that (1) reads each file in the file list; (2) keeps only MOVEMENT_ID, DISPLAY_NAME and geometry columns; (3) adds that file to the predefined void sf object.

for(i in 1:length(file.names))
 {
  file=st_read(file.names[i]) #(1)
  file=select(file, MOVEMENT_ID, DISPLAY_NAME, geometry) #(2)
  output=rbind(out.file, file) #(3) 
  }

With this code, output contains only the last file that was read.

  • Is this a problem with the void sf object?
  • Is it a question of specifying rows? output=rbind(out.file, file[i,]) outputs only one row of the last file.

回答1:


I cannot test it right away but ths should at least point you to the solution in your case.

The problem is that you overwrite output in every iteration, so only the last one is available after the last iteration. You try to do the right thing by using rbind, but you have to use the formerly created file again and basically append the output within each iteration.

for(i in 1:length(file.names)) {
  file <- st_read(file.names[i]) #(1)
  file <- select(file, MOVEMENT_ID, DISPLAY_NAME, geometry) #(2)
  out.file <- rbind(out.file, file) #(3) 
}

A final not: please use arrows as assignment operators. The equal sign should be used for function arguments in R.



来源:https://stackoverflow.com/questions/59738534/r-loop-outputting-only-the-last-iteration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!