我编写一个函数,出现报错:
Error in a1:a2 : NA/NaN argument,没有结果。
我把函数提取出来,处理,也出现同样的报错,但是有结果。
我的问题:
1、为什么报错?
2、为什么程序可以运行出结果,但是编程函数调用就没有结果,什么情况?
程序如下:
to<- c("a",23,3,5,4,"a",23,"a")
c<- NULL
hello <- function(a){
total<- a
location <- grep("a",total)
for(a in seq(location)){
a1 <- (location[a]+1) # near the up number
a2 <- (location[a+1]-1) # near the down number
for (b in (a1:a2)){
c <- to[a]
}
}
}
hello(to)
c
结果:
> hello(to)
Error in a1:a2 : NA/NaN argument
> c
NULL
这是调用函数的结果,c没有被处理,不明白。
单独运行程序:
to<- c("a",23,3,5,4,"a",23,"a")
c<- NULL
total<- to
location <- grep("a",total)
for(a in seq(location)){
a1 <- (location[a]+1) # near the up ck number
a2 <- (location[a+1]-1) # near the down ck number
for (b in (a1:a2)){
c <- to[a]
}
}
c
结果如下:
> for(a in seq(location)){ a1 <- (location[a]+1) # near the up ck number
a2 <- (location[a+1]-1) # near the down ck number
for (b in (a1:a2)){
c <- to[a]
}
}
Error in a1:a2 : NA/NaN argument
> c[1] NA "a" "a" "a" "a" NA "23"
这里c是有结果的,也出现报错信息:NA/NaN argument