我使用的函数返回空结果

时间:2019-04-22 02:56:01

标签: r dplyr

我正在尝试用一个人的名字“猜测”性别。我知道有一个性别包,但是我想使用自己的数据来使用它。

作为一个初学者,我试图复制性别包的代码,但返回空结果。

这是我的名为namestat的数据库。

dput(head(namestat,10))
structure(list(name = c("AABIA", "AABIDA", "AABISH", "AADARSH", 
"AADIA", "AAEISHA", "AAESHA", "AAFAF", "AAFIA", "AAFIRA"), female = c(1, 
2, 1, 2, 1, 1, 1, 1, 19, 1), male = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 0)), row.names = c(NA, 10L), class = "data.frame")

这是代码:

function(names) {

    namestat %>%
        filter(name %in% tolower(names)) %>%
        group_by(name) %>%
        summarise(female = sum(female),
                  male = sum(male)) %>%
        mutate(proportion_male = round((male / (male + female)),
                                       digits = 4),
               proportion_female = round((female / (male + female)),
                                         digits = 4)) %>%
        mutate(gender = ifelse(proportion_female == 0.5, "either",
                               ifelse(proportion_female > 0.5, "female",
                                      "male"))) %>%
        select(name, proportion_male, proportion_female, gender)

}

我希望输出与genderfunc(“ AABIA”)

 name  proportion_male proportion_female gender

  <chr>           <dbl>             <dbl> <chr>  

1 AABIA            0             1     female 

但目前我收到的结果为空。

1 个答案:

答案 0 :(得分:1)

太长,无法添加为评论。使用此功能对我有用:

select_me<-function(nam){
  df %>%
  group_by(name) %>%
  summarise(female = sum(female),
            male = sum(male)) %>%
  mutate(proportion_male = round((male / (male + female)),
                                 digits = 4),
         proportion_female = round((female / (male + female)),
                                   digits = 4)) %>%
  mutate(gender = ifelse(proportion_female == 0.5, "either",
                         ifelse(proportion_female > 0.5, "female",
                                "male"))) %>% 
 dplyr::select(name, proportion_male, proportion_female, gender) %>% 
    filter(name%in%nam)
}
select_me("AABIA")


# A tibble: 1 x 4
  name  proportion_male proportion_female gender
  <chr>           <dbl>             <dbl> <chr> 
1 AABIA               0                 1 female