Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

dataframe - R: Imputing NAs with random choice of female or male

I am working in R.

I have a data frame column with female or male and some NAs.

Now, I want to randomly assign female or male to the NA values in this column. I do not want to have all NAs be male or female but every NA randomly assigned either or.

How do I do that?

Best, corkinabottle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An option is to get an index of the NA elements in the column, then use sample with size specified as the number of NA elements to replace those NA with the male or female sampled

i1 <- is.na(df1$col1)
df1$col1[i1] <- sample(c('male', 'female'), size = sum(i1), replace = TRUE)

-output

df1
#    col1 col2
#1      a    1
#2 female    2
#3      b    3
#4   male    4
#5      a    5
#6      c    6
#7 female    7

data

df1 <- data.frame(col1 = c('a', NA, 'b', NA, 'a', 'c', NA), col2 = 1:7)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share

1.2m questions

2.1m answers

5 comments

56.7k users

...