忽略 sapply 函数中的 NA
我正在使用 R 并四处寻找答案,但虽然我看到过类似的问题,但它对我的具体问题不起作用。
在我的数据集中,我尝试使用 NA
作为占位符,因为一旦完成部分分析,我将返回它们,因此,我希望能够完成所有操作我的计算就好像 NA
并不真正存在一样。
这是我的示例数据表问题,
ROCA = c(1,3,6,2,1,NA,2,NA,1,NA,4,NA)
ROCA <- data.frame (ROCA=ROCA) # converting it just because that is the format of my original data
#Now my function
exceedes <- function (L=NULL, R=NULL, na.rm = T)
{
if (is.null(L) | is.null(R)) {
print ("mycols: invalid L,R.")
return (NULL)
}
test <-(mean(L, na.rm=TRUE)-R*sd(L,na.rm=TRUE))
test1 <- sapply(L,function(x) if((x)> test){1} else {0})
return (test1)
}
L=ROCA[,1]
R=.5
ROCA$newcolumn <- exceedes(L,R)
names(ROCA)[names(ROCA)=="newcolumn"]="Exceedes1"
我收到错误:
Error in if ((x) > test) { : missing value where TRUE/FALSE needed
正如你们所知,sapply 函数出了问题。关于如何忽略这些 NA
有什么想法吗?如果我可以让它将所有 NA
插入到它们之前所在的位置,我会尝试 na.omit
,但我不知道该怎么做。
I am using R and have searched around for an answer but while I have seen similar questions, it has not worked for my specific problem.
In my data set I am trying to use the NA
's as placeholders because I am going to return to them once I get part of my analysis done so therefore, I would like to be able to do all my calculations as if the NA
's weren't really there.
Here's my issue with an example data table
ROCA = c(1,3,6,2,1,NA,2,NA,1,NA,4,NA)
ROCA <- data.frame (ROCA=ROCA) # converting it just because that is the format of my original data
#Now my function
exceedes <- function (L=NULL, R=NULL, na.rm = T)
{
if (is.null(L) | is.null(R)) {
print ("mycols: invalid L,R.")
return (NULL)
}
test <-(mean(L, na.rm=TRUE)-R*sd(L,na.rm=TRUE))
test1 <- sapply(L,function(x) if((x)> test){1} else {0})
return (test1)
}
L=ROCA[,1]
R=.5
ROCA$newcolumn <- exceedes(L,R)
names(ROCA)[names(ROCA)=="newcolumn"]="Exceedes1"
I am getting the error:
Error in if ((x) > test) { : missing value where TRUE/FALSE needed
As you guys know, it is something wrong with the sapply function. Any ideas on how to ignore those NA
's? I would try na.omit
if I could get it to insert all the NA
's right where they were before, but I am not sure how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
您想要结果中包含 NA:s 吗?也就是说,您希望行对齐吗?
似乎只是返回 L >那么 test
就可以工作了。添加列也可以简化(我怀疑“Exeedes1”在某个变量中)。
exceedes <- function (L=NULL, R=NULL, na.rm = T)
{
if (is.null(L) | is.null(R)) {
print ("mycols: invalid L,R.")
return (NULL)
}
test <-(mean(L, na.rm=TRUE)-R*sd(L,na.rm=TRUE))
L > test
}
L=ROCA[,1]
R=.5
ROCA[["Exceedes1"]] <- exceedes(L,R)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
不需要
sapply
和匿名函数,因为>
已经矢量化。指定无效的默认参数值似乎也很奇怪。我的猜测是,您将其用作拼凑,而不是使用
missing
函数。抛出错误而不是返回 NULL 也是一个好习惯,因为当函数返回 NULL 时您仍然需要尝试捕获。There's no need for
sapply
and your anonymous function because>
is already vectorized.It also seems really odd to specify default argument values that are invalid. My guess is that you're using that as a kludge instead of using the
missing
function. It's also good practice to throw an error rather than returnNULL
because you would still have to try to catch when the function returnsNULL
.