Python Numpy where() ~= R / rstats dplyr case_when()

As I continue to work on the 2023 Kaggle RSNA “Abdominal Trauma Detection” competition, I continue to convert segments of others’ Python notebooks into R.

While I have been working on converting and integrating the use of a neural networks notebook provided by the Keras team, a surprisingly strong performer has been this “weighted mean baseline” notebook by Jake S Brusca.

Working on that Python notebook suggested that where I see where(), I am probably looking at a use of the ubiquitous Numpy package and the seeming R equivalent is case_when() from the dplyr package.

I converted the code above into the below using dplyr::case_when() after learning Numpy where() resembles the IF function in Excel:

CONDITION, result if TRUE, RESULT if FALSE

I also used dplyr, this time the select() function, to dynamically acquire a column’s values. {category} here can take on the values like “spleen”. category iterates through all values stored in all_target_categories.

Finally, while I did not use Bing AI above, I am excited to see how well it helps with converting Python to R. The initial solution for a standardize_pixel_array() function I found used a package not available in a Kaggle R notebook and the competition requirements require disabling Internet access.

So I asked Bing AI to give me a solution without use of the dicomr package.

Leave a comment