我创建了以下简单函数:
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
任何想法为何?我正在努力弄清为什么它特别不适用于此功能(而其他功能也很好)。
答案 0 :(得分:3)
您的函数导致将负length.out
参数传递给seq
。例如,
> 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