本文介绍了R:产生以下错误的循环:参数1必须具有名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是R编程语言。我创建了一些数据和以下函数:

#load library 
library(dplyr)
 set.seed(123)
 
 # data
 a1 = rnorm(1000,100,10)
 b1 = rnorm(1000,100,5)
 c1 = sample.int(1000, 1000, replace = TRUE)
 train_data = data.frame(a1,b1,c1)

create_data <- function() {

#manually repeat

#generate random numbers
 random_1 =  runif(1, 80, 120)
random_2 =  runif(1, random_1, 120)
 random_3 =  runif(1, 85, 120)
 random_4 =  runif(1, random_3, 120)

#bin data according to random criteria
train_data <- train_data %>% mutate(cat = ifelse(a1 <= random_1 & b1 <= random_3, "a", ifelse(a1 <= random_2 & b1 <= random_4, "b", "c"))) 

#calculate 60th quantile ("quant") for each bin
final_table = data.frame(train_data %>% group_by(cat) %>%
mutate(quant = quantile(c1, prob = .6)))

#create a new variable ("diff") that measures if the quantile is bigger tha the value of "c1"
final_table$diff = ifelse(final_table$quant > final_table$c1,1,0)

#create a table: for each bin, calculate the average of "diff"
final_table_3 = data.frame(final_table %>% 
  group_by(cat) %>% 
  summarize(
   mean = mean(diff)
  ))

#add "total mean" to this table
final_table_3 = data.frame(final_table_3 %>% add_row(cat = "total", mean = mean(final_table$diff)))

#format this table: add the random criteria to this table for reference
final_table_3$random_1 = random_1

final_table_3$random_2 = random_2

final_table_3$random_3 = random_3

final_table_3$random_4 = random_4

}

我现在正尝试运行此函数5次,并存储/保存所有结果:

res <- bind_rows(replicate(5, create_data(), simplify = FALSE), .id = 'iteration')

但这会产生以下错误:

Error: Argument 1 must have names.

有人能告诉我如何修复此错误吗?

谢谢

推荐答案

create_data()中,最后一个调用是Assignment。我们需要return数据

 create_data <- function() {

  #manually repeat

  #generate random numbers
   random_1 =  runif(1, 80, 120)
  random_2 =  runif(1, random_1, 120)
   random_3 =  runif(1, 85, 120)
   random_4 =  runif(1, random_3, 120)

  #bin data according to random criteria
  train_data <- train_data %>% mutate(cat = ifelse(a1 <= random_1 & b1 <= random_3, "a", ifelse(a1 <= random_2 & b1 <= random_4, "b", "c"))) 

  #calculate 60th quantile ("quant") for each bin
  final_table = data.frame(train_data %>% group_by(cat) %>%
  mutate(quant = quantile(c1, prob = .6)))

  #create a new variable ("diff") that measures if the quantile is bigger tha the value of "c1"
  final_table$diff = ifelse(final_table$quant > final_table$c1,1,0)

  #create a table: for each bin, calculate the average of "diff"
  final_table_3 = data.frame(final_table %>% 
    group_by(cat) %>% 
    summarize(
     mean = mean(diff)
    ))

  #add "total mean" to this table
  final_table_3 = data.frame(final_table_3 %>% add_row(cat = "total", mean = mean(final_table$diff)))

  #format this table: add the random criteria to this table for reference
  final_table_3$random_1 = random_1

  final_table_3$random_2 = random_2

  final_table_3$random_3 = random_3

  final_table_3$random_4 = random_4
  final_table_3
  }

-测试OP的代码

res <- bind_rows(replicate(5, create_data(), simplify = FALSE), .id = 'iteration')
dim(res)
[1] 20  7
 head(res)
  iteration   cat      mean  random_1  random_2 random_3 random_4
1         1     a 0.5993624 116.40209 117.33393 116.1137 119.3511
2         1     b 0.5714286 116.40209 117.33393 116.1137 119.3511
3         1     c 0.6000000 116.40209 117.33393 116.1137 119.3511
4         1 total 0.5990000 116.40209 117.33393 116.1137 119.3511
5         2     a 0.6000000  97.57141  99.29284 115.3154 116.8316
6         2     b 0.5930233  97.57141  99.29284 115.3154 116.8316

这篇关于R:产生以下错误的循环:参数1必须具有名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 07:43