jeudi 30 avril 2020

Is there a way to create lists in r based on matching filenames in a for loop?

(I am very new to R so I've been thinking about problem solving in python terms then trying to translate it, but it hasn't been working-- I need R though, for their raster/GIS capabilities)

My goal is to take a list of filenames which I have generated from a folder (ending in either ***DTM.tif or ***DSM.tif), go through that list trying to traverse the strings in each element to find the filenames that match between the DSM and DTM files, then append the files to new lists which order the DSM files and DTM files respectively.

    filenames

    ##### initialize lists
    dsmlst <- list_along()
    dtmlst <- list_along()

    ##### for loop
    for (i in seq_along(filenames)) {
      n = nchar(i)
      if substr(i, n-7, n) == "DSM.tif" {
        list.append(dsmlst, i)
      } else if substr(i, n-7, n) == "DTM.tif" {
        list.append(dtmlst, i)
      }
    }
    dsmlst
    dtmlst

This is all I have so far, and I haven't been able to get it to work. Any advice?

Aucun commentaire:

Enregistrer un commentaire