if statement - Sum up values only if there is no NA in R -
i want sum values 3 columns only, if no column @ position [i,j] has na value.
i tried if clause, won't work:
if(stat1[i,]!=na&&stat2[i,]!=na&&stat3[i,]!=na) is syntax possible in r?
best regards jochen
there’s function complete.cases that:
cols_of_interest = df[, c('stat1', 'stat2', 'stat3')] sum(cols_of_interest[complete.cases(cols_of_interest), ]) the same can equivalently written na.omit as
sum(na.omit(cols_of_interest)) however, complete.cases implemented native c function, whereas na.omit uses quite inefficient r implementation.
Comments
Post a Comment