ropensci / rotl

Interface to Open Tree of Life API
https://docs.ropensci.org/rotl
Other
39 stars 18 forks source link

Convert TRUE/FALSE columns to logicals in tnrs_match_names #54

Closed dwinter closed 9 years ago

dwinter commented 9 years ago

The data.frame returned by tnrs_match_names contains columns that, as far as I can tell, always contain logical/boolean values. However, they end up being represented as characters in the returned object:

birds <- c("stercorarius_parasiticus", "ficedula_albicollis", "sterna_dougallii", 
          "centrocercus_urophasianus", "phasianus_colchicus", "taeniopygia_guttata", 
          "larus_fuscus")
taxa <- tnrs_match_names(birds)
taxa$approximate_match
[1] "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "TRUE"

It would be nice to be able to use these values to subset the data.frame but that doesn't work at the moment (though of course you could coerce them to locicals):

taxa[taxa$approximate_match,]
     search_string unique_name approximate_match ott_id is_synonym
NA            <NA>        <NA>              <NA>   <NA>       <NA>
NA.1          <NA>        <NA>              <NA>   <NA>       <NA>
NA.2          <NA>        <NA>              <NA>   <NA>       <NA>
NA.3          <NA>        <NA>              <NA>   <NA>       <NA>
NA.4          <NA>        <NA>              <NA>   <NA>       <NA>
NA.5          <NA>        <NA>              <NA>   <NA>       <NA>
NA.6          <NA>        <NA>              <NA>   <NA>       <NA>
     is_deprecated number_matches
NA            <NA>           <NA>
NA.1          <NA>           <NA>
NA.2          <NA>           <NA>
NA.3          <NA>           <NA>
NA.4          <NA>           <NA>
NA.5          <NA>           <NA>
NA.6          <NA>           <NA>

I would have a god at his myself, but don't quite understand the black magic you are weaving to create these dataframes in the first place @fmichonneau!

Can this be done easily? Since they already look like R logicals I wonder if they are being coerced to characters somewhere along the way?

fmichonneau commented 9 years ago

This is fixed but by a workaround. It happens because I use rbind to build the data frame and it converts everything into a string.

This part of the code uses too much "black magic" as you noticed... and I think that using purrr to build the data frame would probably make things easier. I'll try to look into it....

dwinter commented 9 years ago

Nice one @fmichonneau,

When I've run into similar problems, being able to specify rbind.data.frame when collecting up the rows has helped (since they are data frame rows that won't be coerced to one class). Don't know if it would help in this case, just a heads up for something to check out.

fmichonneau commented 9 years ago

unfortunately, it doesn't work in this case :sob: