0

I need some help trying to figure out what is going on with this possibly function. My cast_first list has 65000 names that I need to loop through, but special characters are throwing errors that I need to get around.

I built this function and tried to use mapply to call it, but I'm getting this error

Error in .f(.x[[i]], ...) : unused argument (.x[[i]]) 

Can anyone tell me what I'm doing wrong here and point me in the direction to fix?

get_gender = function(name, year) {
  cast_gender <- gender(name, years = year, method = "ssa") %>% .$gender
  
  if(length(gender_prob) == 0) {
    cast_gender <- "Unknown" %>% append(cast_gender,.)
  } else {
    cast_gender <- append(cast_gender,gender_prob)
  }
}

possibly_gender = function() {
  possibly(mapply(FUN = get_gender, name = cast_first, year = cast_year),otherwise = "NA", quiet = TRUE)
} 

test <- cast_first %>% map(possibly_gender)
JustBeginning
  • 33
  • 1
  • 4
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 06 '21 at 02:15
  • One potential error is the use of `if`..`else` thinking it should succeed in processing a vector input. It doesn't. There are many such question posted on SO. I'll see if I can find a canonical example.Perhaps: https://stackoverflow.com/questions/11865195/using-if-else-on-a-data-frame (Be sure to upvote the question and answer. You need to learn to play the SO game.) – IRTFM Dec 06 '21 at 02:18
  • 1
    Decided that a fairly obvious case of an `if`-`else`// `ifelse` conflation should be closed after I found the excellent answer offered by GGrothendieck showing how it can be used in the `dplyr/magrittr` paradigm. – IRTFM Dec 06 '21 at 02:26

0 Answers0