Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R – Convert columns of type list to type str

I Am using a API call to Clinicaltrail.gov to pull some studies of interest using the code below. I am able to create a dataframe(studylist) with the columns i need , however each of these columns are type List, how can convert all the columns from list to str

library(httr)
library(jsonlite)

res <- httr::GET(
  paste0("https://clinicaltrials.gov/api/query/study_fields?",
         "expr=AREA[Condition](lyme)",
         "&fields=NCTId,",
         "Condition,OfficialTitle,phase,StartDate,EnrollmentCount",
         "&min_rnk=1&max_rnk=10&fmt=json"))
data <- fromJSON(rawToChar(res$content))
studylist <- as.data.frame(data$StudyFieldsResponse$StudyFields)

I tried using unnest from tidyr as follows , this seems to work however i end up with 6 rows instaed of expected 10 rows.

unnest(studylist,c(Phase,NCTId,Condition,OfficialTitle,StartDate,EnrollmentCount))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You lose 4 rows because 4 of your Phases are length-0.

sapply(studylist, lengths)
#       Rank NCTId Condition OfficialTitle Phase StartDate EnrollmentCount
#  [1,]    1     1         1             1     0         1               1
#  [2,]    1     1         1             1     1         1               1
#  [3,]    1     1         1             1     1         1               1
#  [4,]    1     1         1             1     1         1               1
#  [5,]    1     1         1             1     1         1               1
#  [6,]    1     1         1             1     0         1               1
#  [7,]    1     1         1             1     1         1               1
#  [8,]    1     1         1             1     0         1               1
#  [9,]    1     1         1             1     0         1               1
# [10,]    1     1         1             1     1         1               1

We can fix this with:

studylist[] <- lapply(studylist, function(z) { z[lengths(z) == 0] <- NA; z; })
unnest(studylist, everything())
# # A tibble: 10 × 7
#     Rank NCTId       Condition    OfficialTitle  Phase StartDate EnrollmentCount
#    <int> <chr>       <chr>        <chr>          <chr> <chr>     <chr>          
#  1     1 NCT05678478 Lyme Disease Perceptions,S… NA    February… 45             
#  2     2 NCT05634811 Lyme Disease A PHASE 3, RA… Phas… December… 3000           
#  3     3 NCT05477524 Lyme Disease A Phase 3, Mu… Phas… August 4… 18000          
#  4     4 NCT04867473 Lyme Disease Teleyoga for … Not … October … 13             
#  5     5 NCT04863287 Lyme Disease A Phase 1 Stu… Phas… February… 44             
#  6     6 NCT04835792 Lyme Disease Biomarker Stu… NA    March 21… 1000           
#  7     7 NCT04719962 Lyme Disease Diagnosis of … Not … July 7, … 10             
#  8     8 NCT04653558 Lyme Disease Clinical Mani… NA    January … 311            
#  9     9 NCT04422314 Lyme Disease ImmuneSense L… NA    July 9, … 893            
# 10    10 NCT03981874 Lyme Disease Comparative S… Not … May 29, … 50             
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading