Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R filter() dealing with NAs

I am trying to implement Chebyshev filter to smooth a time series but, unfortunately, there are NAs in the data series.

For example,

t <- seq(0, 1, len = 100)                     
x <- c(sin(2*pi*t*2.3) + 0.25*rnorm(length(t)),NA, cos(2*pi*t*2.3) + 0.25*rnorm(length(t)))

I am using Chebyshev filter: cf1 = cheby1(5, 3, 1/44, type = "low")

I am trying to filter the time series exclude NAs, but not mess up the orders/position. So, I have already tried na.rm=T, but it seems there's no such argument. Then

z <- filter(cf1, x)   # apply filter

Thank you guys.

like image 208
Yu Deng Avatar asked Jul 18 '12 12:07

Yu Deng


People also ask

What does filter () do in R?

The filter() method in R is used to subset a data frame based on a provided condition. If a row satisfies the condition, it must produce TRUE . Otherwise, non-satisfying rows will return NA values. Hence, the row will be dropped.

How do you check if there are NAs in R?

To check which value in NA in an R data frame, we can use apply function along with is.na function. This will return the data frame in logical form with TRUE and FALSE.

How do I get rid of NAs in R?

To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).

How do I remove NAs from a column in R?

To remove observations with missing values in at least one column, you can use the na. omit() function. The na. omit() function in the R language inspects all columns from a data frame and drops rows that have NA's in one or more columns.


1 Answers

Try using x <- x[!is.na(x)] to remove the NAs, then run the filter.

like image 86
Eli Sander Avatar answered Sep 28 '22 03:09

Eli Sander