for循环只对r中的最后一行运行



我在R中有一个简单的for循环问题-我试图使它运行整个数据集,它只运行最后一行。这是用非常复杂的数据集完成的,这些数据集都是形状文件,我正在测试几何图形的交集。这就是为什么我不能在这里做一个可复制的例子。

尽管如此,这是我的代码:

for(i in 1:nrow(data1)){
#get intersections between data2 and data1 for specific years
output = st_join(
x    = data1[i, ],
y    = data2[which(data2$year %in% data1$lag.year[i]:data1$year[i]), ],
join = st_intersects
)
#Get area of intersections
output = transform(output, 
inter_area = mapply(function(x, y) {
as.numeric(sf::st_area(
sf::st_intersection(x, y)
))}, x = geometry, y = geom_2))
## obtaining the proportion of area in data1 intersected by data2
output = transform(output, prop_inter = inter_area/area)
#get cycle-specific values
output <- output%>%
group_by(code, year.x)%>%
dplyr::summarise(prop_inter = sum(prop_inter),
end_date= max(end_date),
start_date= max(start_date))%>%
ungroup()
return(output)
}

正如您所看到的,我正在测试data2data1的相交,并查看data1相交的百分比取决于它们在yearlag.year上的值。问题是,当我运行这个,它只返回最后一行所需的结果,而不是整个data1对象。我已经分别测试了循环中所有不同的代码,它们都按照我想要的那样做,但是一旦我尝试将所有代码扩展到整个数据帧,它就只对最后一行执行。

所以我想这一定是我在循环中犯的一些简单愚蠢的错误。

谢谢!

继续重写输出对象;您可能想要创建一个长度为nrow(data)的向量,并将结果赋值给它的第i个元素。我不认为这与{sf}或一般的GIS有关,它更多的是关于for循环和向量在R中的工作方式-考虑这个例子:

for (i in 1:50) {

output <- i # rewriting output object 50 times

}
print(output) # this will be a single element for last row (50)
output <- numeric(50)
for (i in 1:50) {

output[i] <- i # storing result in a new element of output for each i

}
print(output) # this will be 1:50 as expected

您可能想要考虑这样的事情(如果不访问您的数据,很难确定,但它应该让您开始)。

result <- numeric(nrow(data1)) # init the vector
for(i in 1:nrow(data1)){
#get intersections between data2 and data1 for specific years
output = st_join(
x    = data1[i, ],
y    = data2[which(data2$year %in% data1$lag.year[i]:data1$year[i]), ],
join = st_intersects
)
#Get area of intersections
output = transform(output, 
inter_area = mapply(function(x, y) {
as.numeric(sf::st_area(
sf::st_intersection(x, y)
))}, x = geometry, y = geom_2))
## obtaining the proportion of area in data1 intersected by data2
output = transform(output, prop_inter = inter_area/area)
#get cycle-specific values
result[i] <- output%>% # store in i-th element of result instead
group_by(code, year.x)%>%
dplyr::summarise(prop_inter = sum(prop_inter),
end_date= max(end_date),
start_date= max(start_date))%>%
ungroup()
#   return(output)  # no need for return unless you are in a function
}

最新更新