首页 > 解决方案 > 如何使用 R 中的 for 循环通过代码块在列表中运行多个数据帧?

问题描述

territory.name我有一个充满鸟类观察的数据框,我需要获取该数据并为每个已知的命名鸟类区域 ( )创建 .KMV 文件。我有一段代码可以获取位置数据并创建这些 .KMV 文件。手动这涉及为每个命名区域创建一个单独的 .CSV 文件,并通过代码块单独运行它以生成所需的 .KML 文件,但我想使用 R 脚本自动化该过程。

我已经做到了,每个区域都在一个单独的数据框中,使用split(). 这些数据帧的示例如下:

structure(list(season = c(" FA15", " FA15", " FA15", " FA15", 
" FA15"), year = c(2015, 2015, 2015, 2015, 2015), territory.name = c("Rocky", 
"Rocky", "Rocky", "Rocky", "Rocky"), plot = structure(c(10L, 
10L, 10L, 10L, 10L), .Label = c("Buena Vista", "Coches Prietos", 
"Coches Prietos ", "Field Station", "Field Station ", "Isthmus", 
"Navy", "Off-plot", "Offplot", "Portazuela", "Sauces", "Scorpion Ranch", 
"test"), class = "factor"), color.band = c("BKUAP", "BKUAP", 
"BKUAP", "BKUAP", "BKUAP"), lat = c(34.0117649920285, 34.0121079795063, 
34.011702965945, 34.0113220084459, 34.0113999601454), lon = c(-119.758454980329, 
-119.758648015559, -119.758594036102, -119.757832037285, -119.757832037285
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))

structure(list(season = c(" FA15", " FA15", " FA15", " FA15", 
" FA15"), year = c(2015, 2015, 2015, 2015, 2015), territory.name = c("Hades", 
"Hades", "Hades", "Hades", "Hades"), plot = structure(c(3L, 3L, 
3L, 3L, 3L), .Label = c("Buena Vista", "Coches Prietos", "Coches Prietos ", 
"Field Station", "Field Station ", "Isthmus", "Navy", "Off-plot", 
"Offplot", "Portazuela", "Sauces", "Scorpion Ranch", "test"), class = "factor"), 
    color.band = c("PPOAY", "PPOAY", "PPOAY", "PPOAY", "PPOAY"
    ), lat = c(33.972684033215, 33.9726449735463, 33.9726769924164, 
    33.9730469696224, 33.9725800137967), lon = c(-119.723170017824, 
    -119.723048983142, -119.723543012515, -119.723651977256, 
    -119.723950959742)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

最终,此列表中有 19 个不同的数据框,每个命名区域一个。我想我需要在这里使用 for 循环,但以前从未这样做过。到目前为止,这是我学习 Stack Overflow 和阅读文档的内容:

library(adehabitat)
library(rgdal)

df.list <- split(Fall2015_most, Fall2015_most$territory.name)

for(x in 1:length(df.list)){
  
###### Below is the code chunk for creating .KML files #####
# Remove rows with NA's in the lat and lon columns
x <- x[!is.na(x$lat) & !is.na(x$lon),]

# Ensure longitude is negative so it works in the western hemisphere 
x$lon <- x$lon*-

# Create a copy of the object to make into a SpatialPointsDataFrame
# Only include three columns (territory.name, x, and y coordinates) for estimating home ranges
# Make sure columns are listed  lon and lat
x.sp <- x[, c("territory.name", "lon", "lat")]

# Create a SpatialPointsDataFrame by defining the coordinates and long lat projection
coordinates(x.sp) <- c("lon", "lat")
proj4string(x.sp) <-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
str(x.sp)

kernel.ref <- kernelUD(x.sp, h = "href")  # href = the reference bandwidth
image(kernel.ref) # plot
kernel.ref[[1]]@h # The smoothing factor is stored for each animal in the "h" slot
kernel.lscv <- kernelUD(x.sp, h = "LSCV") # Least square cross validation
image(kernel.lscv) # plot

par("mar")
par(mar=c(1,1,1,1))

plotLSCV(kernel.lscv) # Look for a dip

x.kernel.poly <- getverticeshr(kernel.ref, percent = 70) 
print(x.kernel.poly)  # returns the area of each polygon

# Transform the point and MCP objects. 
x.spgeo <- spTransform(x.sp, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
x.bvkernalgeo <- spTransform(x.kernel.poly, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

# Write data to a KML file that can be viewed in Google Earth
writeOGR(x.spgeo, "x.kml", layer="id", driver="KML", verbose=TRUE)
writeOGR(x.bvkernalgeo, "xBVkernel.kml", layer="id", driver="KML", verbose=TRUE)

} # close bracket for x

当我运行它时,我得到了错误

Error: $ operator is invalid for atomic vectors

在行中

for (x in 1:length(df.list))

如何让代码为每个数据帧运行。我将用于通过代码手动运行每个区域的文件名已替换为 x。我的最终目标是df.list通过代码成功且自动地成功运行每个数据帧。谢谢!

标签: rdataframefor-looprgdaladehabitathr

解决方案


推荐阅读