mlachans
mlachans

Reputation: 49

How to fill empty 0 x n tibble with NA

I am using the gender package in R and unfortunately the gender function in that package returns blank tibbles when it cannot classify a name as male or female.

Is there a "purrr"- style way to apply the gender function so that empty tibbles of size n x m are replaced by NAs of size n x m in my output, so as to keep the row-size of the inputs and outputs to the gender function equal?

I would like to find a solution that does not involve writing a wrapper for the gender function (if possible).

Upvotes: 0

Views: 335

Answers (1)

neilfws
neilfws

Reputation: 33772

I'd approach this by storing the names in a data frame column, then joining the results from gender() back to the original data.

For example:

library(gender)

mydata <- data.frame(name = c("Neil", "Askey"), stringsAsFactors = FALSE)

merge(mydata, gender(mydata$name), all = TRUE)

Result:

   name proportion_male proportion_female gender year_min year_max
1 Askey              NA                NA   <NA>       NA       NA
2  Neil          0.9964            0.0036   male     1932     2012

Upvotes: 1

Related Questions