代码之家  ›  专栏  ›  技术社区  ›  RobertF

R错误:x[[jj]]中出错<-v:运行函数时尝试在integerOneIndex中选择少于一个元素

  •  0
  • RobertF  · 技术社区  · 5 年前

    当我尝试在测试数据集上运行以下函数代码段时,我看到了上述错误:

    df <- data.frame(id=c(1,2,3,4), actual=c(0,1,0,1), score=c(.7223,.8904,.0037,.0025), prediction=c(1,0,0,1),
                     black=c(.6476,.0534,.0702,NA), hispanic=c(.1406,.8262,.0645,NA), asian=c(.1100,.0141,.7930,NA),
                     white=c(.0444,.0740,.0056,NA), female=c(0,1,0,1), male=c(1,0,1,0), weight=c(.1755,.8101,.1332,.6420))
    
    adverse_impact_ratio <- function (
      data_frame,
      lower_outcome_favorable,
      outcome,
      true_outcome,
      pg_names,
      cg_names,
      sample_weight,
      air_threshold
    ) {
    
      data <- data_frame
      
      if (is.null(sample_weight)) {
        data['sample_weight'] <- 1
        sample_weight <- 'sample_weight'
      }
      
      if (lower_outcome_favorable) {
        data[outcome] <- 1 - data[outcome]
        if (!is.null(true_outcome)) {
          data[true_outcome] <- 1 - data[true_outcome]
        }
      }
    }
    
    adverse_impact_ratio(
      df, 
      1, 
      df$score, 
      df$actual, 
      pg_names=c('black', 'hispanic', 'asian', 'female'), 
      cg_names=c('white', 'white', 'white', 'male'), 
      df$weight, 
      0.9) 
    

    知道我做错了什么吗?我在这个论坛上也看到过类似的问题,但它们通常涉及for循环,这里不是这样。谢谢!

    1 回复  |  直到 5 年前
        1
  •  1
  •   brunomp    5 年前

    尝试: 不利影响比( 1, “实际”, pg_names=c('黑人','西班牙裔','亚裔','女性'), cg\u names=c('白色','白色','白色','男性'), df$重量,

    推荐文章