0

I want to write a single df with the freq of 150 raster objects (partly answered in: Count freq of multiple raster objects in R)

I have created a list of all the raster files with

spatial.tools::list.raster.files() and then called

lapply(ls$raster,freq)

Now I have a list containing 150 entries that contain a freq matrix for every raster file.

I am only interested in $Band.1[,"count"]) however. For a single entry of the list I can create a df for counts with

as.data.frame(all[[1]]$Band.1[,"count"])

My question is: How can I write $Band.1[,"count"] for all of the 150 in the list into a single df in one go???

loening
  • 13
  • 3

1 Answers1

0

I see you're new here. Others will have an easier time answering your question if you can make your question reproducible -- check out this post on how to make a great reproducible example. That being said, using your other question, this should likely get you what you need:

library(tidyverse)

list_of_results <- lapply(ls$raster,freq)

df_of_results <- 
  list_of_results %>%
  map_df(~ data.frame(.))

df_of_results$count

If the tidyverse and purrr::map functions aren't your thing, you could also do something like:

results <- unlist(lapply(list_of_results, function(x) x[, c("count")]))
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • You're right im new here. Thanks for the advice I'll try to incorporate it. ```df_of_results <- list_of_results %>% map_df(~ data.frame(.))``` is working just fine. ```results <- unlist(lapply(list_of_results, function(x) x[, c("count")]))``` throws this error: "Error in x[, c("count")] : incorrect number of dimensions" – loening Sep 20 '18 at 09:18
  • Another problem is that the ouput of the first solution is a data frame with a single column for four different values.. Does the ```purr::map```function offer a way to make it 4 columns instead of a single one? – loening Sep 20 '18 at 09:29
  • @loening I think for all of these questions, it would really be easier for others to help you with a reproducible example. Use fake/dummy data to illustrate your problem, what you've tried, and what you expect the output to look like. – JasonAizkalns Sep 20 '18 at 13:18