How to replace values with 0 ?

Dear All,

I wanted to replace highest value in data frame (e.g a = data.frame(10,2,3,99,4,56,9,10) ) to 0, and also I want to convert surrounding index values ( e.g max(a) = 99 , so I want to convert surrounding 5 index values on each side of maximum value to 0).

I tired to replace it this way,

> a = data.frame(10,2,3,4,56,9,10,99)
> a = as.numeric(a)
> a
> [1] 10  2  3  4 56  9 10 99
> a = replace(a,c((which(a==max(a))-1):(which(a==max(a))+1)),0)
> a
> [1] 10  2  3  4 56  9  0  0  0
> 

But this approach is changing the size of my data frame.

In my real data the size of data frame always changes and max(value of data frame) could be first or even last value. But I want to usually replace max(data.frame) and its surrounding (+40 and -40) index values to be zero without changing the size of data frame.

I am having am error when I am trying to replace some index values in a data frame to 0.

Error in x[list] <- values : only 0's may be mixed with negative subscripts

I figured out why this error is coming.

Could you help me in solving this problem.

Thank You,

Best Regards
Shri

Are you sure that your data is structured like your example i.e. you have one value per column?

a <- data.frame(10,2,3,4,56,9,10,99)

head(a)
#>   X10 X2 X3 X4 X56 X9 X10.1 X99
#> 1  10  2  3  4  56  9    10  99

Created on 2020-06-29 by the reprex package (v0.3.0)

(a <- c(10,2,3,4,56,9,10,99,#added some more
        10,2,3,4,56,9,10))

library(tidyverse)
(df <- enframe(a,
               name = "index",
               value="a"))
 
(max_value <- max(a))
(index_max_value<-which(a==max_value))


(result_df <- mutate(df,
       new_a = if_else(condition = between(x = index,
                       left=index_max_value-5,
                       right=index_max_value+5),
               true = 0,
               false = a)
))

Well, in real data of my is of (m x n) = (2050 x 1) that is column vector.

But just for understanding I used data.frame(10,2,3,4,56,9,10,99).

Hey can we do this using basic library instead of tidyverse ?

But that is not a column vector. If your example is meant to represent the contents of a single column, then it should be like this:

a <- data.frame(col1 = c(10, 2, 3, 4, 56, 9, 10, 99))

head(a)
#>   col1
#> 1   10
#> 2    2
#> 3    3
#> 4    4
#> 5   56
#> 6    9

Created on 2020-06-29 by the reprex package (v0.3.0)

The structure of your data will determine the type of solution required.

1 Like

I use dplyr library in my code and now when I install tidyverse i have

library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.0 --
v ggplot2 3.2.1 v purrr 0.3.3
v tibble 3.0.1 v dplyr 0.8.4
v tidyr 1.0.3 v stringr 1.4.0
v readr 1.3.1 v forcats 0.5.0
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()

(a <- c(10,2,3,4,56,9,10,99,#added some more
        10,2,3,4,56,9,10))


(df <- data.frame(a=a,
                  index=1:length(a)))
 
(max_value <- max(a))
(index_max_value<-which(a==max_value))


(df$new_a <- ifelse(test = index_max_value-5 <= df$index &
                      index_max_value+5 >= df$index,
               yes = 0,
               no = a)
)
1 Like

Hey, sorry yes the structure of data should be as you mentioned.

Thank you. This code works.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.