简体   繁体   English

是否可以循环遍历多个对象并调用循环中每个对象内的元素

[英]Is it possible to loop over multiple objects and call to elements within each object in the loop

In RI would like to loop over a set of three functions, with the output requiring saving from each function with a name related to the input. 在RI中,想要循环一组三个函数,输出需要从每个函数中保存,其名称与输入相关。 This works when applied to one file but I would like to loop over 300+ objects and the function requires specifying elements within the object. 这适用于一个文件,但我想循环超过300个对象,该函数需要指定对象中的元素。

I attempted to create lists of the objects and output names and looping over it with a for loop for a single function (a.ppp) and received an error "Error in i[["X"]] : subscript out of bounds". 我尝试创建对象和输出名称的列表,并使用for循环为单个函数(a.ppp)循环,并收到错误“i [[”X“]中的错误]:下标超出范围”。 I am very new to for loops and have limited coding background and am unsure if the loop structure i have created is correct. 我对for循环很新,编码背景有限,不确定我创建的循环结构是否正确。 I have tried multiple options including looping over a dataframe or nesting loops based on some other stack overflow questions. 我尝试了多种选择,包括循环数据帧或基于其他一些堆栈溢出问题嵌套循环。

Some toy data, representing my setup. 一些玩具数据,代表我的设置。 I have dataframes eg. 我有数据帧,例如。 ag 股份公司

a <- data.frame(X = c(1, 2, 3),
            Y = c(3,2,1),
            Z = c(4,5,6),
            M = c('A', 'B', 'C'))

I would like to loop over the following three functions. 我想循环以下三个函数。

library(spatstat)

a.ppp = ppp(a$X,a$Y,c(0,3),c(0,3),marks = a$M)
a.nnd = nndist(a.ppp,by=a.ppp$marks)
a.append = cbind(a,a.nnd)

My Attempt has included 我的尝试包括在内

listObj = c("a","b","c","d","e","f","g")
list.ppp = c("a.ppp","b.ppp","c.ppp","d.ppp","e.ppp","f.ppp","g.ppp")

for (i in listObj) {
for (j in list.ppp) {

   j=ppp(i[["X"]],i[["Y"]],c(0,12),c(0,12),marks=i[["M"]])
  }
}

I recieved the error: 我收到了错误:

#Error in i[["X"]] : subscript out of bounds

My Expected results would be a .ppp and .append output for a to g 我的预期结果是a到g的.ppp和.append输出

Just Thought I'd Follow up, Based on the extremely helpful comment from Joran. 只是想到我会跟进,基于Joran非常有帮助的评论。 I have figured the issue out through a modification of his provided code. 我通过修改他提供的代码来解决问题。 The code I used was as follows 我使用的代码如下

library(spatstat)

 a <- data.frame(X = c(1, 2, 3),
                            Y = c(3,2,1),
                            Z = c(4,5,6),
                            M = c('A', 'B', 'C')) 

 #Create a list of all the vectors in the environment - Not an ideal method but 
 suitable for the case

 dfs= mget(ls())

 #Create empty lists to be populated during the loop
 dfs_ppp = list()
 dfs_nnd = list()
 dfs_final= list()


for (i in seq_along(dfs)){
 dfs_ppp[[i]] <- ppp(dfs[[i]]$X,dfs[[i]]$Y,c(-1,14),c(-1,14),marks = dfs[[i]]$M)
 dfs_nnd[[i]] = nndist(dfs_ppp[[i]],by=dfs_ppp[[i]]$marks)
 dfs_final[[i]] = cbind(dfs[[i]],dfs_nnd[[i]])  
 }

Try something more like this: 尝试更像这样的东西:

library(spatstat)

a <- data.frame(X = c(1, 2, 3),
                                Y = c(3,2,1),
                                Z = c(4,5,6),
                                M = c('A', 'B', 'C'))

# Put your data frames (a, b, c, etc.) in a list
dfs <- list(x = a,b = a,z = a)

for (i in seq_along(dfs)){
    ppp_obj <- ppp(dfs[[i]]$X,dfs[[i]]$Y,c(0,3),c(0,3),marks = dfs[[i]]$M)
    nnd_obj = nndist(ppp_obj,by=ppp_obj$marks)
    dfs[[i]]$nnd <- nnd_obj
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM