NinjaR
NinjaR

Reputation: 623

Update values of a column based on predefined thresholds

I have a data set as follows

Name    Price
A       100
B       123
C       112
D       114
E       101
F       102

I need a way to update the value in the price column if the price is between +3 or -3 of a vector of values specified to the value specified in the vector. The vector may contain any number of elements.

Vector = c(100,111)

Updated dataframe

Name    Price
A       100
B       123
C       111
D       111
E       100
F       100

If the vector is

Vector = c(104,122) 

then the updated dataframe needs to be

Name    Price
A       100
B       122
C       112
D       114
E       104
F       104

Upvotes: 0

Views: 30

Answers (2)

CPak
CPak

Reputation: 13591

Here's one approach

bound <- 3
upper_bound <- Vector+bound
lower_bound <- Vector-bound
vi <- Reduce("pmax", lapply(seq_along(Vector), function(i) i*(df$Price <= upper_bound[i] & df$Price >= lower_bound[i])))
# [1] 1 0 2 2 1 1
vi_na <- replace(vi, vi == 0, NA)
# [1]  1 NA  2  2  1  1
df$Price <- dplyr::mutate(df, Price = ifelse(is.na(Vector[vi_na]), Price, Vector[vi_na]))

  # Name Price.Name Price.Price
# 1    A          A         100
# 2    B          B         123
# 3    C          C         111
# 4    D          D         111
# 5    E          E         100
# 6    F          F         100

Data

df <- read.table(text = "Name    Price
A       100
B       123
C       112
D       114
E       101
F       102", header=TRUE)

Vector = c(100,111)   

Upvotes: 2

Marc P
Marc P

Reputation: 352

df <- data.frame('Name' = LETTERS[1:6], 'Price'= c(100,123,112,114,101,102))


transform <- function(value, conditionals){

    for(cond in conditionals){
        if(abs(value - cond) < 4){
            return(cond)
        }
     }

    return(value)
}

sapply(df$Price, transform, c(104,122))

This should work. It can probably done in one line with apply (but I find it difficult to read sometimes so this should be easier to read).

Upvotes: 2

Related Questions