jeudi 8 février 2018

R replace empty column of DF with random categorical value

Trying to replace demographic values by assigning them randomly.

I can obtain empty gender data rows by carrying out following:

df$gender[df$gender == "",]

user_id, name, age, gender
001, xyz, 23,  
004, abc, 32, 

I want to assign gender randomly:

sample(c('male', 'female'), nrow(df$gender[df$gender == ""]), replace=TRUE, prob=c(0.5, 0.5))

tried following:

df$gender[df$gender == ""] <- sample(c('male', 'female'), nrow(df$gender[df$gender == ""]), replace=TRUE, prob=c(0.5, 0.5))

This only assigned to few cells but not all.




Aucun commentaire:

Enregistrer un commentaire