为 R 中的列表中的列表进行的计算迭代分配变量名?
Iteratively assign variable names for calculations done on list within a list in R?
我在 R studio 中遇到问题已经有一段时间了,我想先声明一下,我对 R 还是很陌生,仍然不知道所有“最快”的做事方法,但有任何帮助耐心会很棒。
我是一名研究人员,必须计算不同气象站的天气模型的技能分数,并且有数千个数据点需要迭代,我认为最简单的方法是在文件中读取并将每个文件存储为列表中的单独列表。
这样做之后,我会循环遍历列表中的每个列表,并计算不同的错误分数(正常错误、RMSE 等...),这样我就不必复制和粘贴大约 121 个不同的循环。
这种做事方法显然是 A) 不是一个好方法,或者 B) 我对做这件事的最佳方法没有把握。
所以,下面是一个简单的例子:
# The weather model forecasts
model1 <- c(1:15)
model2 <- c(1:15)
model3 <- c(1:15)
# What actually happened (verification/measured values)
verification_model1 <- c(1:15)
verification_model2 <- c(1:15)
verification_model3 <- c(1:15)
# List of lists containging total model data
totalmodeldata <- list(model1,model2,model3)
totalverificationdata <-list(verification_model1,verification_model2,verification_model3)
# Calculate normal error
for (val in totalmodeldata){
for (ob in totalverificationdata){
uniquevarname <- val - ob # here is the issue!
}
}
所以这是我的一个问题,使它变得简单 Q/A...
- 是否有一种简单的方法可以在列表计算中为这些单独的列表分配唯一的变量名?因为就目前而言,代码执行了它应该进行的错误计算,但只存储了它计算的最后一个列表 (model3.
理想情况下,它会计算 model1 VS 的正常误差。 verification_model1 将数据分配给一个变量,然后移动到第二个列表,依此类推,这样我就有了 model1、model2 和 model3 的正常错误。
更新:
好的,我们正在取得进展。这是我想要的输出......
在将模型数据(model1..2..3 等)和验证数据(verifcation_model1..2..3 等)声明到“总”列表后,for 循环或类似函数将循环并对每个模型和验证数据列表执行以下减法:
unqiue_var_error1 <- model1 - verification_model1
unqiue_var_error2 <- model2 - verification_model2
unqiue_var_error3 <- model3 - verification_model3
output/stored 数据看起来像是每个模型的简单数据向量...这是我工作中正常错误的实际输出:
> head(model1,n = 15L)
[1] -6 0 1 0 -4 -2 2 0 -3 -2 -3 -5 -5 -5 -5
这样,一旦循环完成,每个模型都会出现正常错误。
这是一个使用嵌套 purrr::map2
.
的解决方案
(您实际上并不需要嵌套映射,它只允许将结果包装在单个数据框中。)
library(tidyverse)
map2_df(totalmodeldata, totalverificationdata,
function(model, verify) {
map2_dbl(model, verify, ~.x - .y)
}
)
# A tibble: 10 x 3
model1 model2 model3
<dbl> <dbl> <dbl>
1 -1.22 -0.611 -1.50
2 1.82 0.911 1.71
3 3.06 1.53 3.84
4 -1.40 -0.700 -1.37
5 -1.33 -0.665 -1.27
6 1.57 0.787 2.43
7 0.252 0.126 0.482
8 1.77 0.886 1.14
9 -0.200 -0.100 -0.543
10 0.642 0.321 0.419
数据:
# added randomness to avoid having all zeros show up in diff scores
# The weather model forecasts
set.seed(123)
n <- 10
model1 <- rnorm(n)
model2 <- model1 * .5
model3 <- model1 * 1.5
# What actually happened (verification/measured values)
set.seed(234)
verification_model1 <- rnorm(n)
verification_model2 <- verification_model1 * .5
verification_model3 <- verification_model1 * 1.5
# List of lists containging total model data
totalmodeldata <-
list(model1, model2, model3) %>% set_names('model1', 'model2', 'model3')
totalverificationdata <-
list(verification_model1, verification_model2, verification_model1) %>%
set_names('ver_model1', 'ver_model2', 'ver_model3')
注意:purrr
包是基于 R apply
系列函数的 tidyverse(根据 OP 评论,它与 R >= 3.2 兼容)。
我在 R studio 中遇到问题已经有一段时间了,我想先声明一下,我对 R 还是很陌生,仍然不知道所有“最快”的做事方法,但有任何帮助耐心会很棒。
我是一名研究人员,必须计算不同气象站的天气模型的技能分数,并且有数千个数据点需要迭代,我认为最简单的方法是在文件中读取并将每个文件存储为列表中的单独列表。
这样做之后,我会循环遍历列表中的每个列表,并计算不同的错误分数(正常错误、RMSE 等...),这样我就不必复制和粘贴大约 121 个不同的循环。 这种做事方法显然是 A) 不是一个好方法,或者 B) 我对做这件事的最佳方法没有把握。
所以,下面是一个简单的例子:
# The weather model forecasts
model1 <- c(1:15)
model2 <- c(1:15)
model3 <- c(1:15)
# What actually happened (verification/measured values)
verification_model1 <- c(1:15)
verification_model2 <- c(1:15)
verification_model3 <- c(1:15)
# List of lists containging total model data
totalmodeldata <- list(model1,model2,model3)
totalverificationdata <-list(verification_model1,verification_model2,verification_model3)
# Calculate normal error
for (val in totalmodeldata){
for (ob in totalverificationdata){
uniquevarname <- val - ob # here is the issue!
}
}
所以这是我的一个问题,使它变得简单 Q/A...
- 是否有一种简单的方法可以在列表计算中为这些单独的列表分配唯一的变量名?因为就目前而言,代码执行了它应该进行的错误计算,但只存储了它计算的最后一个列表 (model3.
理想情况下,它会计算 model1 VS 的正常误差。 verification_model1 将数据分配给一个变量,然后移动到第二个列表,依此类推,这样我就有了 model1、model2 和 model3 的正常错误。
更新: 好的,我们正在取得进展。这是我想要的输出...... 在将模型数据(model1..2..3 等)和验证数据(verifcation_model1..2..3 等)声明到“总”列表后,for 循环或类似函数将循环并对每个模型和验证数据列表执行以下减法:
unqiue_var_error1 <- model1 - verification_model1
unqiue_var_error2 <- model2 - verification_model2
unqiue_var_error3 <- model3 - verification_model3
output/stored 数据看起来像是每个模型的简单数据向量...这是我工作中正常错误的实际输出:
> head(model1,n = 15L)
[1] -6 0 1 0 -4 -2 2 0 -3 -2 -3 -5 -5 -5 -5
这样,一旦循环完成,每个模型都会出现正常错误。
这是一个使用嵌套 purrr::map2
.
的解决方案
(您实际上并不需要嵌套映射,它只允许将结果包装在单个数据框中。)
library(tidyverse)
map2_df(totalmodeldata, totalverificationdata,
function(model, verify) {
map2_dbl(model, verify, ~.x - .y)
}
)
# A tibble: 10 x 3
model1 model2 model3
<dbl> <dbl> <dbl>
1 -1.22 -0.611 -1.50
2 1.82 0.911 1.71
3 3.06 1.53 3.84
4 -1.40 -0.700 -1.37
5 -1.33 -0.665 -1.27
6 1.57 0.787 2.43
7 0.252 0.126 0.482
8 1.77 0.886 1.14
9 -0.200 -0.100 -0.543
10 0.642 0.321 0.419
数据:
# added randomness to avoid having all zeros show up in diff scores
# The weather model forecasts
set.seed(123)
n <- 10
model1 <- rnorm(n)
model2 <- model1 * .5
model3 <- model1 * 1.5
# What actually happened (verification/measured values)
set.seed(234)
verification_model1 <- rnorm(n)
verification_model2 <- verification_model1 * .5
verification_model3 <- verification_model1 * 1.5
# List of lists containging total model data
totalmodeldata <-
list(model1, model2, model3) %>% set_names('model1', 'model2', 'model3')
totalverificationdata <-
list(verification_model1, verification_model2, verification_model1) %>%
set_names('ver_model1', 'ver_model2', 'ver_model3')
注意:purrr
包是基于 R apply
系列函数的 tidyverse(根据 OP 评论,它与 R >= 3.2 兼容)。