temor
temor

Reputation: 1029

How to deal with NA in sens.slope in r?

If I have this data:

library(trend)
v <- runif(100) * 1:100
func <- function(x,na.rm=T) { unlist(sens.slope(x)) }
func(v,na.rm=T)

everything went right and no problem. But in my real data there is NA.

v[1]=NA
func(v,na.rm=T)

I got this error: Error in na.fail.default(x) : missing values in object

Upvotes: 1

Views: 883

Answers (1)

M. Rodo
M. Rodo

Reputation: 486

Set v <- v[!is.na(v)] first (before supplying v to func) and it should run fine.

Or else adjust your function, in which case the above is unnecessary:

func <- function(x,na.rm=T) {
 if(sum(is.na(x)) == length(x)) return(NA)
 if(na.rm) x <- x[!is.na(x)]
 unlist(sens.slope(x)) 
}

Upvotes: 1

Related Questions