如何迭代保存结果格式的列表? [英] How to iterate over a list preserving the format of the results?
问题描述
我有一个简单的函数来创建列表中的光栅文件列表及其名称(以.grd格式,在下面的例子中称为list_names):
<$ ()函数(list_names){
raster_list< - list()#初始化栅格列表
for( i in 1:(length(list_names))){
grd_name< - list_names [i]#list_names包含.grd格式的所有图像名称
raster_file< - raster(grd_name)
raster_list< - append(raster_list,raster_file)#每次迭代更新raster_list
}
#应用函数
raster_list < -lapply(list_names,FUN = ListRasters)
期望的结果应该是格式:
[[1]]
class:RasterLayer
#etc
[ [2]]
class:RasterLayer
#etc
他们的格式为:
[[ 1]]
[[1]] [[1]]
class:RasterLayer
#etc
$ b [[2]]
[[2 ]] [[1]]
class:RasterLayer
#etc
是一个问题,因为以后我不能访问栅格列表上的项目。
我找不到解决方案,因为我不明白为什么迭代会给出这种格式的结果。你可以给我一些解释或建议如何解决这个功能,或者你可以看到我犯了什么错误?
任何帮助都是值得欢迎的! / p>
谢谢!!
循环。不过, lapply
会返回一个列表,也许你应该试试 sapply
。
raster_list< -sapply(list_names,FUN = ListRasters)
$ b $
$ b
编辑:
我认为这应该会给你想要的东西。如果你把一个最小的可重复的例子,因为我们不知道数据是怎么样的,什么是 raster
函数会更好,但下面是一个玩具的例子 lapply
和 sapply
。
PS :看来你的公式迭代是不正确的,但你将不得不提供更多的信息,所以我们可以修复公式。
list_names <-c(a,b,c,d)#defining list_names
#defining the function
ListRasters< - function list_names){
raster_list< - list()#初始化栅格列表
for(i in 1: st_names))){
grd_name< - list_names [i]#list_names包含.grd格式的所有图像名称
raster_file< - paste(file,grd_name,sep =。 )
raster_list< - append(raster_list,raster_file)#每次迭代更新raster_list
}
使用 lapply
:
raster_list< -lapply(list_names,FUN = ListRasters)
输出结果是:
[[1]]
[[1]] [[1]]
[1] file.a
[[2]]
[[2]] [[1]]
[1]file.b
$ b [[3]]
[[3]] [[1]]
[1]file.c
[[4]]
[[4]] [[1]]
[1]file.d
使用 sapply
:
<$ c
$ b输出结果是: raster_list <-sapply(list_names,FUN = ListRasters)
$ a
[1]file.a
$ b [1]file.b
[1]file.c
$ d
[1]file.d
I have a simple function to create a list of raster files from a list with their names (in .grd format, and called list_names in the example below):
ListRasters <- function(list_names) { raster_list <- list() # initialise the list of rasters for (i in 1:(length(list_names))){ grd_name <- list_names[i] # list_names contains all the names of the images in .grd format raster_file <- raster(grd_name) } raster_list <- append(raster_list, raster_file) # update raster_list at each iteration } # Apply the function raster_list <-lapply(list_names, FUN = ListRasters)
The desired result should be in the format:
[[1]] class : RasterLayer # etc [[2]] class : RasterLayer # etc
But instead I get them in the format:
[[1]] [[1]][[1]] class : RasterLayer # etc [[2]] [[2]][[1]] class : RasterLayer # etc
That is a problem because later on I can not access the items on the raster list. I can't find a solution because I don't understand why the iteration gives this format of result. Can you give me some explanation or some advice on how to fix the function, or can you see where I am making a mistake?
Any help is more than welcome!
Thankyou!!
解决方案It seems that something is odd with your loop. Nevertheless, the
lapply
is going to return a list, maybe you should trysapply
.raster_list <-sapply(list_names, FUN = ListRasters)
I think this should give you what you want.
EDIT:
It would be better if you had put a minimal reproducible example, for we do not know how the data is and what is the
raster
function, but below follows a toy example with the differences of output oflapply
andsapply
.PS: and it does seem that your formula iteration is not correct, but you would have to provide more information so we can fix the formula.
list_names <- c("a", "b","c", "d") #defining the list_names #defining the function ListRasters <- function(list_names) { raster_list <- list() # initialise the list of rasters for (i in 1:(length(list_names))){ grd_name <- list_names[i] # list_names contains all the names of the images in .grd format raster_file <- paste("file", grd_name, sep=".") } raster_list <- append(raster_list, raster_file) # update raster_list at each iteration }
Using
lapply
:raster_list <-lapply(list_names, FUN = ListRasters)
The output is:
[[1]] [[1]][[1]] [1] "file.a" [[2]] [[2]][[1]] [1] "file.b" [[3]] [[3]][[1]] [1] "file.c" [[4]] [[4]][[1]] [1] "file.d"
Using
sapply
:raster_list <-sapply(list_names, FUN = ListRasters)
The output is:
$a [1] "file.a" $b [1] "file.b" $c [1] "file.c" $d [1] "file.d"
这篇关于如何迭代保存结果格式的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!