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 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()