【问题标题】:mapply error: 'length.out' must be a non-negative numbermapply 错误:“length.out”必须是非负数
【发布时间】:2020-06-19 14:29:21
【问题描述】:

我创建了以下简单函数:

fillampenv <- function(samples, samprate, rise, fall){
  # Create output vector
  v <- vector("numeric", samples)
  # Fill output vector
  v <- c(seq(0, 1, length = rise * samprate),
         seq(1, 1, length = (((samples/samprate) -
                                (rise + fall)) * samprate) -1),
         seq(1, 0, length = fall * samprate))
  return(v)
}

我想对数据框的每一行调用:

df <- structure(list(samples = c(17640, 17640, 17640, 17640, 17640), samprate = c(44100, 44100, 44100, 44100, 44100), rise = c(0.75, 0.75, 0.75, 0.75, 0.75), fall = c(0.3, 0.3, 0.3, 0.3, 0.3)), class = "data.frame", row.names = c(NA, -5L))

但是,当我尝试使用以下方法执行此操作时(这适用于我制作的其他功能):

ampenvs <- mapply(fillampenv,
                  samples = df$samples,
                  samprate = df$samprate,
                  rise = df$rise,
                  fall = df$fall)

我得到错误:

Error in seq.default(1, 1, length = (((samples/samprate) - (rise + fall)) *  : 
  'length.out' must be a non-negative number

任何想法为什么?我正在努力弄清楚为什么它不能特别适用于这个功能(而其他人工作得很好)。

【问题讨论】:

    标签: r apply mapply


    【解决方案1】:

    您的函数导致传递给seq 的负length.out 参数。例如,

    > df %>% mutate(x = (((samples/samprate) -
    +                       (rise + fall)) * samprate) -1)
      samples samprate rise fall      x
    1   17640    44100 0.75  0.3 -28666
    2   17640    44100 0.75  0.3 -28666
    3   17640    44100 0.75  0.3 -28666
    4   17640    44100 0.75  0.3 -28666
    5   17640    44100 0.75  0.3 -28666
    

    【讨论】:

      【解决方案2】:

      在遇到此问题的任何地方,您都必须将您的数字解析为您想要的数字,例如 Float、Integer 和 ...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        • 1970-01-01
        • 2021-08-11
        • 1970-01-01
        • 2020-05-05
        • 1970-01-01
        相关资源
        最近更新 更多