更新时间:2023-11-23 15:39:04
(至少对于一维向量)[
似乎返回 NA
当 "i > length(x)" 而 [[
返回错误.
(at least regarding 1D vectors) [
seems to return NA
when "i > length(x)" whereas [[
returns an error.
x = runif(5)
x[6]
#[1] NA
x[[6]]
#Error in x[[6]] : subscript out of bounds
挖掘一下,do_subset_dflt
(即[
)调用ExtractSubset
我们注意到当一个想要的索引 ("ii") 是 ">length(x)" NA
被返回(稍微修改为干净):
Digging a bit, do_subset_dflt
(i.e. [
) calls ExtractSubset
where we notice that when a wanted index ("ii") is "> length(x)" NA
is returned (a bit modified to be clean):
if(0 <= ii && ii < nx && ii != NA_INTEGER)
result[i] = x[ii];
else
result[i] = NA_INTEGER;
另一方面do_subset2_dflt
(即 [[
) 如果想要的索引 ("offset") 是 ">length(x)" 则返回错误(稍微修改为干净):
On the other hand do_subset2_dflt
(i.e. [[
) returns an error if the wanted index ("offset") is "> length(x)" (modified a bit to be clean):
if(offset < 0 || offset >= xlength(x)) {
if(offset < 0 && (isNewList(x)) ...
else errorcall(call, R_MSG_subs_o_b);
}
where #define R_MSG_subs_o_b _("下标越界")
(我不确定上面的代码片段,但根据他们的回报,它们确实是相关的)
(I'm not sure about the above code snippets but they do seem relevant based on their returns)