dimanche 24 octobre 2021

Making a data.frame rows to be randomly different in R

I want to randomly delete rows of my data based on given variables.

For example, if I give you the numerical variable group, then randomly delete all rows with the maximum value of group (i.e., 2) in any random study.

For example, if I give you the character variable outcome, then randomly delete all rows with any one value of outcome (either "A" or "B") in any random study.

If I give you both group and outcome, then both deletions should NOT happen to the same study because it may lead to the complete removal of that study from the data.

Is this possible in R?

m="
study group outcome
    1     1 A      
    1     1 B      
    1     2 A      
    1     2 B      
    2     1 A      
    2     1 B      
    2     2 A      
    2     2 B      
    3     1 A      
    3     1 B      
    3     2 A      
    3     2 B      
    4     1 A      
    4     1 B      
    4     2 A      
    4     2 B      
    5     1 A      
    5     1 B      
    5     2 A      
    5     2 B 
"
data <- read.table(text = m, h = T)



Aucun commentaire:

Enregistrer un commentaire