如何将列表的一部分写入一个数据帧? [英] How can I write parts of a list into one data frame?

查看:83
本文介绍了如何将列表的一部分写入一个数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用150个栅格对象的频率写一个df (部分回答: 计算R中多个栅格对象的频率)

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() 然后叫

lapply(ls$raster,freq)

现在,我有一个包含150个条目的列表,其中每个栅格文件都包含一个频率矩阵.

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

但是我只对$Band.1[,"count"])感兴趣. 对于列表的单个条目,我可以使用

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"])

我的问题是: 我如何一次将列表中的所有150个$Band.1[,"count"]写入单个df中?

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???

推荐答案

我看到你是新来的.如果您可以使问题可重复,其他人将可以更轻松地回答您的问题-在如何制作出色的可重复示例中查看此帖子.话虽这么说,使用另一个问题,这可能会为您带来所需的东西:

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

如果tidyversepurrr::map函数不是您要的,则还可以执行以下操作:

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")]))

这篇关于如何将列表的一部分写入一个数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆