r - 使用 R 函数创建多个数据集
问题描述
我创建了以下函数,它采用 3 个数字参数,经度大小(以度为单位)、纬度大小(以度为单位)和年份。该函数创建大小由前两个参数表示的正方形(网格),然后将数据集中的观察结果分配到这些网格上,按年份分隔(第三个参数)。该功能按预期工作。
要使用该函数构建 2009 年的 2x2 组合(包含所有观测值的网格),我调用:
assemblage_2009 <- CreateAssembleage(2, 2, 2009)
但是,我想从 2009 年到 2018 年迭代地创建组装。
我试图做一个 for 循环i in 2009:2018
但没有多大成功。我也尝试过lapply
,但也没有多大成功。
更有经验的 R 用户有什么想法吗?
功能:
CreateAssembleage <- function(size_long, size_lat, year){
# create a dataset to hold only values with the chosen year
data_grid_year <- dplyr::filter(data_grid, Year == year)
# Create vectors to hold the columns (easier to work with)
Longitude <- data_grid_year$Longitude
Latitude <- data_grid_year$Latitude
dx <- size_long # set up the dimensions (easier to change here than inside the code)
dy <- size_lat
# construct the grids
gridx <- seq(min(Longitude), max(Longitude), by = dx) # the values we discussed for the big square
gridy <- seq(min(Latitude), max(Latitude), by = dy)
# take the data and create 3 new columns (x, y, cell) by finding the specified data inside the constructed grids
grid_year <- data_grid_year %>%
mutate(
x = findInterval(Longitude, gridx),
y = findInterval(Latitude, gridy),
cell = paste(x, y, sep = ",")) %>%
relocate(Sample_Id, Latitude, Longitude, x, y, cell) # bring forward the new columns
### Create the assemblage
data_temp <- grid_year %>%
group_by(cell) %>% # group by the same route id
select(-c(Sample_Id, Latitude, Longitude, Midpoint_Date_Local,
Year, Month, Chlorophyll_Index, x, y)) %>% # remove unneeded columns
summarise(across(everything(), sum)) # calculate the sum
return(data_temp) #return the result
}
谢谢大家的任何想法。
解决方案
我无法检查您的功能是否有效,因为我没有您提供的任何数据。这就是说,调用一个函数 n 次并保存输出有多种可能性。由于您没有指定问题,我不得不假设您很难在循环中运行该函数并保存输出。另外,我必须假设 1st: 你的功能有效,2nd:size_long
并且size_lat
总是设置为 2。如果你有不同的想法,你必须更清楚你想要什么。
一些选项:
- 使用输出创建一个列表
lapply
。请注意,此处必须size_long = 2, size_lat = 2
在定义函数时进行设置,因此这些值是标准值。此外,提出year
第一个论点。
years <- 2009:2018
results <- lapply(years, CreateAssembleage)
- 使用 for 循环创建带有输出的列表:
results <- list()
for(i in 2009:2018){
list[[paste0("assemblage_", i)]] <- CreateAssembleage(size_long = 2, size_lat = 2, year = i)
}
- 如果需要,创建多个变量,每年一个:
for(i in 2009:2018){
do.call("<-", list(paste0("assemblage_", i), CreateAssembleage(size_long = 2, size_lat = 2,
year = i)))
}
- 与 3 相同,但使用
assign
:
for(i in 2009:2018){
assign(paste0("assemblage_", i), CreateAssembleage(size_long = 2, size_lat = 2, year = i))
}
请注意,如果您每次不仅要更改year
其他变量,还要更改其他变量,例如,size_lat
每次迭代都更改,则必须使用mapply
而不是lapply
,或者,如果是循环,则必须创建向量(或一个数据框)以及其他变量并调整您的循环。
编辑:按照 MrFlick 的建议,我更改了选项的顺序并添加了assign
-option。对于大多数初学者来说,循环更容易理解,但对于大型数据集来说,它们可能会慢得令人讨厌。所以最好习惯lapply
。
推荐阅读
- lua - 在lua中查找两个字符串之间的字符串
- texas-instruments - 我的 TI-84 Plus CE-T 计算器是否可以以 surd 形式显示?
- python - Bron-Kerbosch 算法在无向图中找到最大团
- javascript - 如何在我的 Javascript 代码中捕获来自 Metamask 浏览器扩展的锁定/解锁事件?
- java - 如何知道请求是否通过 oauth2 授权发出?
- javascript - Nodemon - 使用配置文件指定扩展监视列表
- apache-camel - 骆驼升级引入了 SSL 问题
- javascript - 如何删除按钮 onclick 周围的默认橙色突出显示?
- godot - 如何在释放运动键时停止运动体行走?
- sql - 在 FROM 子句中的子查询内重用连接表中的字段