mercredi 23 septembre 2020

randomly replacing percentage of values per group with NA in R dataframe

I have a dataframe with different groups (ID) of varying size. Within each group, I would like to randomly replace a specific percentage of values in the "value" column (let's say 30%) with NA's. Here is a simplified version of my data:

ID<-rep(c("X1","X2"),times=c(3,6)
value<-c(1,2,3,1,2,3,4,5,6)
data.frame(ID,value)
ID value
X1     1
X1     2
X1     3
X2     1
X2     2
X2     3
X2     4
X2     5
X2     6

Here is what I would like to have:

ID value
X1     1
X1     NA
X1     3
X2     1
X2     2
X2     NA
X2     4
X2     5
X2     NA

Any idea how I could do this? I have a preference for using tidyverse but if you have other options, that would also be appreciated!




Aucun commentaire:

Enregistrer un commentaire