问题
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???
回答1:
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")]))
来源:https://stackoverflow.com/questions/52388444/how-can-i-write-parts-of-a-list-into-one-data-frame