【问题标题】:Question about passing sapply index to function关于将 sapply 索引传递给函数的问题
【发布时间】:2021-01-29 13:52:00
【问题描述】:

我确信这是一个简单的问题。但我是编程新手,所以我很挣扎。我认为我想要完成的工作应该从代码中非常清楚。本质上,我想生成一个长度为 i 的随机数向量,检查是否有少于 i 个唯一数字。我想多次这样做作为一种模拟。当我这样做时,我手动使用以下代码:

experiment<- function() {
          ab <- rdunif(i, 1, 365)
          ab <- data.frame(ab)
          count <- uniqueN(ab)
          if (count < i)
            return(1)
          else
            return(0)
        }
                
        vector <- replicate(10, experiment(), simplify=FALSE)
        sum <- sum(as.data.frame((vector)))
        probability <- sum/(10)

它工作正常。但我需要运行这个模拟 40 次,我宁愿不手动进行。但是,我似乎无法为我工作,我无法弄清楚我做错了什么:

i<-10:50
    experiment<- function(i) {
      ab <- rdunif(i, 1, 365)
      ab <- data.frame(ab)
      count <- uniqueN(ab)
      if (count < i)
        return(1)
      else
        return(0)
    }
    
    complete <- function(i) {
    
    vector <- replicate(10, experiment(i), simplify=FALSE)
    sum <- sum(as.data.frame((vector)))
    probability <- sum/(10)
    
    return(probability)
    }
    
    sapply(i, complete(i), simplify=FALSE)

这是我目前遇到的错误: match.fun(FUN) 中的错误: 'complete(i)' 不是函数、字符或符号 另外:警告信息:

1: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
2: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
3: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
4: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
5: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
6: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
7: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
8: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
9: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used
10: In if (count < i) return(1) else return(0) :
  the condition has length > 1 and only the first element will be used

【问题讨论】:

  • 不确定函数 uniqueN 是什么,但其余部分看起来像 R。R 的唯一函数返回一个向量,而不是一个标量(唯一值)。因此,错误只是告诉您从 count 数组的第一个值中选择了 Experiment() 的返回值(b/c 它还能做什么?)。将 count
  • 当使用sapply()时,它的FUN参数只是函数名,即你需要像sapply(i, complete, simplify=FALSE)一样调用它

标签: r function loops sapply


【解决方案1】:

我想通了:

    experiment<- function(i) {
  ab <- rdunif(i, 1, 365)
  count <- length(unique(ab))
  if (count < i) return(1)
  else return(0)
}

i <- 10:50

replication <- function(i) {
  replicate(100, experiment(i))
}

data<- sapply(i, replication)

colMeans(data)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多