1

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.

JRR
  • 3,024
  • 2
  • 13
  • 37
Sher
  • 369
  • 2
  • 19

1 Answers1

1

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)
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Thank you, but it returns `Error in data.frame(File = x, roughness = roughness) : object 'x' not found` – Sher Oct 29 '19 at 10:20
  • Oh, there's a bug in your original code. I have edited mine now. Should work – StupidWolf Oct 29 '19 at 11:02
  • Thanks, after removing commas at the end of each line in function, code is running but returning error. `Error in C_rasterize(las, layout, subcircle, 1L) : C++ unexpected internal error in 'rasterize': point of raster.` For single file it works fine. Do you have any idea why this is happening? – Sher Oct 30 '19 at 02:19
  • Oh yes, you don't need the comma. Sorry I don't have lidR installed or the files. The error is coming from your readLAS functions.. Maybe it doesn't work with one of your files – StupidWolf Oct 30 '19 at 08:16
  • so you figured out the error of readLAS? and everything worked? – StupidWolf Nov 04 '19 at 23:03