仅当R中不存在NA时,求和数值

仅当R中不存在NA时,求和数值,r,if-statement,R,If Statement,如果在[I,j]位置没有列有NA值,我只想对3列的值求和 我尝试将其用于if子句,但它不起作用: if(stat1[i,]!=NA&&stat2[i,]!=NA&&stat3[i,]!=NA) 在R中这样的语法可能吗 致意 Jochen有这样的功能: cols_of_interest = df[, c('stat1', 'stat2', 'stat3')] sum(cols_of_interest[complete.cases(cols_of_interes

如果在[I,j]位置没有列有NA值,我只想对3列的值求和

我尝试将其用于if子句,但它不起作用:

  if(stat1[i,]!=NA&&stat2[i,]!=NA&&stat3[i,]!=NA)
在R中这样的语法可能吗

致意 Jochen

有这样的功能:

cols_of_interest = df[, c('stat1', 'stat2', 'stat3')]
sum(cols_of_interest[complete.cases(cols_of_interest), ])
同样可以用
na等效地编写。省略
as

sum(na.omit(cols_of_interest))

但是,
complete.cases
是作为本机C函数实现的,而
na.omit
目前使用的R实现效率相当低。

请提供示例数据和所需输出。每行的行和有一个
行和
函数……
na.omit()
也应该可以工作,但我记得比
完成要慢。cases
@AnandaMahto你在这两个方面都是对的,我现在真的很困惑
na.ommit
并不是简单地用
完成来实现的。cases
,这看起来应该是微不足道的。相反,
na.omit
使用了一个非常天真的R实现。