I simply want to replace outlier values (those above the 99th percentile) by NA for each group individualy. I dont know how to do that without creating an entirely new dataframe which is redundant. Any ideas ?

```
group <- c('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B')
var1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 50)
df = data.frame(group, var1)

unique_groups = unique(df$group)

df2 = data.frame()

for(g in 1:length(unique_groups)) {

  subset_df <- df[grep(unique_groups[1], df$group), ]

  quantiles <- quantile(subset_df$var1, c(.01, .99), na.rm = TRUE)

  subset_df$var1[subset_df$var1 > quantiles[2]] <- NA

  df2 <- rbind(df2, subset_df)
}

```

Thank you,