我想循环一个向量并将值作为参数发送到函数.我想发送的值之一是NULL.这就是我一直在尝试的
things <- c('M','F',NULL)
for (thing in things){
doSomething(thing)
}
Run Code Online (Sandbox Code Playgroud)
但循环忽略NULL值.有什么建议?
Jos*_*ich 10
循环不会忽略它.看看,things你会发现它NULL不存在.
您不能在向量中混合类型,因此您不能在同一向量中同时包含两者"character"和"NULL"类型.请改用列表.
things <- list('M','F',NULL)
for (thing in things) {
print(thing)
}
[1] "M"
[1] "F"
NULL
Run Code Online (Sandbox Code Playgroud)
当您使用构建向量时c(),会忽略NULL值:
things <- c('M','F',NULL)
things
[1] "M" "F"
Run Code Online (Sandbox Code Playgroud)
但是,如果重要的是通过NULL下游,则可以list改用:
things <- list('M','F',NULL)
for (thing in things){
print(thing)
}
[1] "M"
[1] "F"
NULL
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
601 次 |
| 最近记录: |