Simon Harmel
Simon Harmel

Reputation: 1489

Replace non NA values of a column in a dataframe with a constant

In my data below, I want to replace only the NONE NA values of column yi whose index are given by ind by Tuke_max value, is this possible (leaving the NA values untouched)?

Fully reproducible code is below.

data <- read.csv("https://raw.githubusercontent.com/ilzl/i/master/j.csv")

first_third_QR <- with(data, fivenum(yi, na.rm = TRUE))[c(2,4)]

inter_QR <- with(data, IQR(yi, na.rm = TRUE))

Tukey_min <- first_third_QR[1] - (3 * inter_QR )
Tukey_max <- first_third_QR[2] + (3 * inter_QR )

ind <- with(data, yi < Tukey_min | yi > Tukey_max)

( yi_vec <- with(data, yi[ind]) ) # Replace the none NA `yi` values below in the `data` 
                                  # with Tukey_max (leave NAs untouched)

#[1] 4.122057 3.457194 3.071523 3.545027 4.454427       NA       NA       NA
#[9]       NA       NA       NA       NA       NA       NA       NA 4.119231 
#[17] 3.977800 6.412906

Upvotes: 0

Views: 1050

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76651

Two things need to be done, to get the conjuntion of ind with the non NA and to index by numeric values in order to not have NA's as indices.
A one-liner will do it.

data$yi[ which(ind & !is.na(data$yi)) ] <- Tukey_max

Upvotes: 1

Related Questions