Sher
Sher

Reputation: 415

How to write multiple filenames as a row in dataframe

I am new to R. I am trying to apply a function to several files and write a dataframe which includes all the file names as one column and corresponding "roughness" result as another column.

library(lidR)
files <- list.files(path= "/allfiles", pattern= "*.laz", full.names = TRUE, recursive = FALSE)

O = lapply(files, function(x)) {

  las = readLAS(x, select = "xyzicnrRGB", filter = "keep_first -drop_z_below 0"),
  chm = grid_canopy(las, 0.2, p2r()),
  roughness <- rumple_index(chm),

  return(roughness)

}

Any help much appreciated.

Upvotes: 1

Views: 93

Answers (1)

StupidWolf
StupidWolf

Reputation: 46958

I think you are almost there. You can use a dataframe. I don't have your file or the library lidR installed, so hopefully rumple_index doesn't return anything too cranky

library(lidR)
files <- list.files(path= "/allfiles", pattern= "*.laz", full.names = TRUE, recursive = FALSE)

O = lapply(files, function(x) {

  las = readLAS(x, select = "xyzicnrRGB", filter = "keep_first -drop_z_below 0")
  chm = grid_canopy(las, 0.2, p2r())
  roughness <- rumple_index(chm)
  return(data.frame(file=x,roughness=roughness))

})
O = do.call(rbind,O)

Upvotes: 1

Related Questions