字符串替换为列表

String replace with list

我快到了。我想获取一个字符串向量并应用以下条件,这将再次 return 一个向量。我在第三步遍历列表时遇到问题。

数据样本:

> assignment
 [1] "female"                                       "female"                                      
 [3] "female"                                       "female"                                      
 [5] "male"                                         "female, female"                              
 [7] "female"                                       "female, female, female"                      
 [9] "female"                                       "female"                                      
[11] "female"                                       "female"                                      
[13] "female"                                       "female"                                      
[15] "female"                                       "female"                                      
[17] "female, female, female, female, female, male" "male"                                        
[19] "female"                                       NA                                      
  

我做了什么:

#Split each row into multiple strings
genders <- str_split(assignment, ", ")

#Assign 1 for female and 0 for male
females_count <- lapply(genders, str_count, pattern="female")

#Count how many women are in each list element
females_sum <- lapply(females_count, sum)

这是我卡住的地方,我想根据以下规则创建另一个长度与 assignment 相同的向量:

compare_genders <- function(females_count,females_sum){

  ifelse (females_sum==1,
  "single_female",
  ifelse(females_sum>1&&females_count!=0,
  "mult_males",
  ifelse(females_sum==0&&length(females_count)==1,
  "single_male",
  ifelse(females_sum==0&&length(females_count)>1,
  "mult_males",
  ifelse(females_sum>1&&length(females_count)>1,
  "mixed_genders","strange"))))
  
}

compare_genders(females_count = females_count,females_sum = females_sum)

但随后我收到此错误消息:

Error in ifelse(females_sum[[i]] == 1, "single_female", ifelse(females_sum[[i]] >  : 
  unused arguments ("mult_males", ifelse(females_sum[[i]] == 0 && length(females_count[[i]]) == 1, "single_male", ifelse(females_sum[[i]] == 0 && length(females_count[[i]]) > 1, "mult_males", ifelse(females_sum[[i]] > 1 && length(females_count[[i]]) > 1, "mixed_genders", "strange"))))

所需矢量示例:

> new_vector
 [1] "single_female"                                       "single_female"                                      
 [3] "single_female"                                       "single_female"                                      
 [5] "single_male"                                         "mult_females"                              
 [7] "females"                                       
 "mult_females"                      
 [9] "single_female"                                       "single_female"                                      
[11] "single_female"                                       "single_female"                                      
[13] "single_female"                                       "single_female"                                      
[15] "single_female"                                       "single_female"                                      
[17] "mixed_genders"
"single_male"                                        
[19] "single_female"                                       NA                                     

 
#Split each row into multiple strings
genders <- str_split(assignment, ", ")
#Assign 1 for female and 0 for male
females_count <- lapply(genders, str_count, pattern="female")
#Count how many women are in each list element
females_sum <- lapply(females_count, sum)

compare_genders <- function(females_count,females_sum){
  ifelse (females_sum==1&&length(females_count)==1,
  "single_female",
  ifelse(females_sum==length(females_count)&&females_sum!=0,
  "mult_females",
  ifelse(females_sum==0&&length(females_count)==1,
  "single_male",
  ifelse(females_sum==0&&length(females_count)!=0,
  "mult_males",
  ifelse(females_sum!=length(females_count),
  "mixed_genders","strange")))))
}
new_vector <- mapply(compare_genders,females_count,females_sum)