Hi, I tried to create a custom function to look up descriptions from diagnostic codes using a web service.
The function works if given a single input, but not if used within mutate. Does this have to do with it not being a vectorized function? This is beyond my depth. Any input appreciated!
library(tidyverse)
library(httr)
# Testing tibble
codes <- tibble::tribble(
~patient_id, ~icd_10_code,
1, "L03.115",
2, "G89.18",
3, "T69.022A",
4, "S78.119A",
5, "L97.518"
)
# My function
ICD_description_API <- function(code) {
url <- paste0("http://icd10api.com/?code=",code,"&desc=long&r=json")
response <- GET(url)
parsed_object <- jsonlite::fromJSON(
content(
response,
as = "text"
),
simplifyVector = FALSE
)
return(parsed_object$Description)
}
# Works
ICD_description_API("T33.09XA")
# Doesn't work
codes %>%
mutate(
description = ICD_description_API(icd_10_code)
)