简体   繁体   中英

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 nxm are replaced by NAs of size nxm 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM