gpt4 book ai didi

R - if 语句中的参数长度为零

转载 作者:行者123 更新时间:2023-12-01 16:19:05 28 4
gpt4 key购买 nike

我对 R 有一点问题,我不知道为什么。它告诉我这一行:if(temp > data[[k]][[k2]]) {参数长度为 0。这是不是那么大的块:

for(k in 1:length(data)) { 
temp <- 0
for(k2 in 3:length(data[[k]])) {
print(data[[k]][[k2]])
if(temp > data[[k]][[k2]]) {
temp <- data[[k]][[k2]]
}
fMax[k] <- temp
k2 <- k2 + 1
}
k <- k + 1
}

data[[k]][[k2]] 中的内容示例:
[1] "3050"
[1] "3051"
[1] "3054"
[1] "3054"
[1] "3052"
[1] "3053"
[1] "3059"
[1] "3059"
[1] "3057"
[1] "3060"
[1] "3063"
[1] "3060"
[1] "3068"
[1] "3067"
[1] "3079"
[1] "3085"
[1] "3094"
[1] "3107"
[1] "3121"
[1] "3135"
[1] "3147"
[1] "3161"
[1] "3200"
[1] "3237"
[1] "3264"
[1] "3274"
[1] "3284"
[1] "3289"
[1] "3292"
[1] "3300"
[1] "3301"
[1] "3303"
[1] "3306"
[1] "3310"
[1] "3312"
[1] "3313"
[1] "3319"
[1] "3314"
[1] "3318"
[1] "3318"
[1] "3320"
[1] "3322"
[1] "3322"
[1] "3322"
[1] "3328"
[1] "3332"
[1] "3338"
[1] "3350"
[1] "3358"
[1] "3378"
[1] "3395"
[1] "3402"
[1] "3875"
[1] "3950"
[1] "3988"
[1] "4018"
[1] "4039"
[1] "4048"
[1] "4057"
[1] "4062"
[1] "4067"
[1] "4076"
[1] "4082"
[1] "4085"
[1] "4092"
[1] "4098"
[1] "4099"
[1] "4101"
[1] "4107"
[1] "4119"
[1] "4139"
[1] "4164"
[1] "4231"
[1] "4347"
[1] "4559"

谢谢 - 山姆

最佳答案

“参数长度为零”是一个非常具体的问题,它来自我最不喜欢的 R 元素之一。让我演示一下这个问题:

> FALSE == "turnip"
[1] FALSE
> TRUE == "turnip"
[1] FALSE
> NA == "turnip"
[1] NA
> NULL == "turnip"
logical(0)

如您所见,与 NULL 的比较不仅不会产生 bool 值,而且根本不会产生值 - 并且控制流倾向于期望检查会产生某种输出。当它们产生零长度输出时......“参数的长度为零”。

(我有一个很长的咆哮,为什么这让我如此愤怒。它可以等待。)

所以,我的问题; sum(is.null(data[[k]])) 的输出是什么? ?如果它不是 0,则您的数据集中嵌入了 NULL 值,并且需要删除相关行,或将检查更改为
if(!is.null(data[[k]][[k2]]) & temp > data[[k]][[k2]]){
#do stuff
}

希望这有帮助;没有整个数据集很难说。如果它没有帮助,并且问题不是 NULL 值进入某个地方,恐怕我不知道。

关于R - if 语句中的参数长度为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27350636/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com