1

I am using the following code. I create a list of first names and then generate links to an API for each name and then try to capture the data from each link.

mydata$NameGenderURL2 <- paste ("https://gender-api.com/get?name=",mydata$firstname, "&key=suZrzhrNJRvrkWFXAG", sep="")

mynamegenderfunction <- function(x){
  GET(url= mydata$NameGenderURL2[x])
  this.raw.content <- genderdata$content
  this.raw.content <- rawToChar(genderdata$content)
  this.content <- fromJSON(this.raw.content)
  name1[x] <- this.content$name
  gender1[x] <- this.content$gender}


namelist <- mydata$firstname[1:100]
genderdata <- lapply(namelist, mynamegenderfunction)

Oddly enough I receive the following message:

>Error in curl::curl_fetch_memory(url, handle = handle) : 
>Could not resolve host: NA` 

I tried another API and got the same issue. Any suggestions?

Here is a data sample: namesurl

https://api.genderize.io/?name=kaan
https://api.genderize.io/?name=Joan
https://api.genderize.io/?name=homeblitz
https://api.genderize.io/?name=Flatmax
https://api.genderize.io/?name=BRYAN
https://api.genderize.io/?name=James
https://api.genderize.io/?name=Dion
https://api.genderize.io/?name=Flintu
https://api.genderize.io/?name=Adriana

The output that I need is the gender for each link, which would be :Male/Female, Null

useR
  • 179
  • 3
  • 13
  • Are there weird characters on your firstname field? You don't seem to be escaping them in any way when building your URL. When asking for help, you should 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 May 16 '18 at 20:50
  • Hello, I added a sample of data – useR May 16 '18 at 21:00
  • This still isn't complete. Too many variables aren't defined. But what do you think is being passed as `x` to your function? Maybe add in a `print(x)` so you can see what's going on. I don't think you are passing the correct value to `GET()`. – MrFlick May 16 '18 at 21:04
  • > print(x) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 [42] 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 [83] 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 – useR May 16 '18 at 21:07
  • It is strange. Once I try GET(url= mydata$NameGenderURL2[1]) it works but as soon as I give it x it gives me error. (mydata$NameGenderURL2 is the list that I pasted above. you can simply check a link and you will see what the api provides) – useR May 16 '18 at 21:09
  • WHen you call `lapply(namelist, mynamegenderfunction)`, you are passing in `namelist`. you are not passing in those numbers. (i'm not sure where you ran that `print(x)`, but I was suggesting you put it inside your `mynamegenderfunction` function. – MrFlick May 16 '18 at 21:10
  • Ok, What it prints is a the very first name in my list (i.e Kaan). I just want x to be a number that goes from 1 to 100. Then it simply brings the corresponding URL – useR May 16 '18 at 21:19
  • Then don't pass names to `lapply`. If you want numbers, try `lapply(seq_along(namelist), mynamegenderfunction)` – MrFlick May 16 '18 at 21:20

0 Answers0