Home > OS >  r: How to change/replace value on random 5% of a factor?
r: How to change/replace value on random 5% of a factor?

Time:07-27

Lets say that I have

set.seed(1)
df <- data.frame(
  WHO = as.factor(sample(1:3, size = 6000, replace = TRUE, prob = c(0.80, 0.15, 0.05)))
)

Which gives

> table(df$WHO)

   1    2    3 
4753  923  324 

I want to replace the value on random 5% of the df$WHO == 1, so these random 5% are allocated to df$WHO == 2.

Expected output

> table(df$WHO)

   1     2    3 
4515  1161  324

It is important that the 5% are chosen completely at random.

CodePudding user response:

Totally at random... within WHO==1

tmp=which(df$WHO==1)
df$WHO[sample(tmp,round(0.05*length(tmp))]=2
  • Related