I would like to substitute the NA values for the average values of the previous and back rows. Moreover, when the first or last lines are NA values, I would just like to repeat the next one before the lines, respectively. My real data has negative and decimal values.
My input:
1.0 NA 1.0 NA 2.0 2.0 3.0 3.0 NA
My expected result:
1.0 2.0 1.0 2.0 2.0 2.0 3.0 3.0 2.0
Hooray!
na.approx zoo. , ( @flodel), NA. na.locf.
na.approx
zoo
NA
na.locf
y <- na.approx(x) y[nrow(y), ] <- na.locf(y[(nrow(y)-1):nrow(y), ])[2, ] y[1, ] <- na.locf(y[1:2,], fromLast=TRUE)[1, ]
EDIT: @ , . :
na.approx(x, rule=2)
( ):
x <- matrix(c(2, NA, 3, NA, 2, 3, 1, 2, NA), 3, 3) p <- rbind(tail(x, -1), NA) # a matrix of previous value n <- rbind(NA, head(x, -1)) # a matrix of next value m <- matrix(rowMeans(cbind(as.vector(p), as.vector(n)), na.rm = TRUE), nrow(x)) # replacements ifelse(is.na(x), m, x)
Pretty simple to decide:
library(imputeTS) na.interpolation(x)
Here it is.
Source: https://habr.com/ru/post/1538382/More articles:How do you get jQuery to wait until the previous event is fired - javascriptHow does the Regex (? = ^.) And ^ (? =.) Engine work? - regexDeploy using Capistrano + Gitlab with: remote_cache - gitEmail Using Special Characters Using Exchange Web Services - .netПоиск фильтра WooCommerce по категориям и настраиваемому полю - phperror: Element has an invalid value" NoExtensions "- c ++Runtime block files - javaHow can I integrate parallel_test and sphinx thinking in cucumber tests? - ruby | fooobar.comПрочтите DIN (идентификационный номер водителя) со смарт-карты (карточки водителя) - serial-numberBootstrap carousel fades, gliding left in the first image - javascriptAll Articles