{"id":17961228,"url":"https://github.com/erictleung/unnestifelse","last_synced_at":"2025-03-25T03:31:37.605Z","repository":{"id":72464236,"uuid":"218180186","full_name":"erictleung/unnestIfElse","owner":"erictleung","description":":hourglass_flowing_sand: RStudio Addin to quickly and roughly convert nested ifelse() statements to dplyr::case_when()","archived":false,"fork":false,"pushed_at":"2019-11-06T20:44:56.000Z","size":133,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-19T08:40:05.312Z","etag":null,"topics":["case-when","dplyr","ifelse","r","rstats","rstudio","rstudio-addin"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erictleung.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-29T01:39:55.000Z","updated_at":"2024-01-28T13:17:37.000Z","dependencies_parsed_at":"2024-01-23T11:15:12.991Z","dependency_job_id":null,"html_url":"https://github.com/erictleung/unnestIfElse","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erictleung%2FunnestIfElse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erictleung%2FunnestIfElse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erictleung%2FunnestIfElse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erictleung%2FunnestIfElse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erictleung","download_url":"https://codeload.github.com/erictleung/unnestIfElse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245394751,"owners_count":20608122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["case-when","dplyr","ifelse","r","rstats","rstudio","rstudio-addin"],"created_at":"2024-10-29T11:08:41.918Z","updated_at":"2025-03-25T03:31:37.598Z","avatar_url":"https://github.com/erictleung.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"# unnestIfElse [![Build Status](https://travis-ci.org/erictleung/unnestIfElse.svg?branch=master)](https://travis-ci.org/erictleung/unnestIfElse)\n\nRStudio Addin to quickly and roughly convert nested `ifelse()` statements to\n`dplyr::case_when()`.\n\n![Demonstration of add-in](./inst/media/nested_if_else.gif)\n\n**Contents**\n\n- [Installation and requirements](#installation-and-requirements)\n- [Before and after](#before-and-after)\n- [Motivation](#motivation)\n- [Features](#features)\n- [Limitations](#limitations)\n- [Examples](#examples)\n- [TODO](#todo)\n- [License](#license)\n\n\n\n## Installation and requirements\n\n**Install**\n\n```r\ndevtools::install_github(\"erictleung/unnestIfElse\")\n```\n\n**Requirements**\n\n```\ndplyr\nmagrittr\nstringr\nrstudioapi\n```\n\n\n## Before and after\n\n**Before**:\n\n```\nifelse(statement, TRUE, FALSE)\n```\n\n**After**:\n\n```\ncase_when(\n  statement ~ TRUE\n  TRUE ~ FALSE\n)\n```\n\n\n## Motivation\n\nInspired by:\n\n\u003cblockquote class=\"twitter-tweet\"\u003e\u003cp lang=\"en\" dir=\"ltr\"\u003e\u003ca href=\"https://twitter.com/hashtag/rstats?src=hash\u0026amp;ref_src=twsrc%5Etfw\"\u003e#rstats\u003c/a\u003e does someone have a magic function to turn nested ifelse statements into dplyr::casewhen?\u003c/p\u003e\u0026mdash; Gordon Shotwell (@gshotwell) \u003ca href=\"https://twitter.com/gshotwell/status/1187783316038148096?ref_src=twsrc%5Etfw\"\u003eOctober 25, 2019\u003c/a\u003e\u003c/blockquote\u003e\n\n\n## Features\n\n- Changes highlighted text in place\n- Highlighted text should be complete `ifelse()` expression\n\n\n## Limitations\n\n- Only works for highlighted text, cannot convert an entire script\n- Highlighted text has to exclusively include complete `ifelse()` statement\n- Lacking any error handling\n- Doesn't handle the case if the last \"no\" value is some function call with\n  parentheses (e.g., `ifelse(1 != 1, -4, abs(-4))`)\n- Limited test coverage for use\n- Code formatting will fail with any text with commas (see below)\n\n```r\n# Addin will fail with this example\n# Source:\n# https://github.com/Sean-Hughes/Relational_Complexity_Derivation/blob/master/Analyses/independent%20t%20test%20-%20IAT%20D1.R\nnhst \u003c- ifelse(t_test_p \u003c 0.05, \n               paste(\"A Welch's independent t test demonstrated significant differences of \", d_interpretation, \" effect size in \", DV_name, \" between \", sep = \"\"),\n               paste(\"A Welch's independent t test demonstrated non-significant differences of \", d_interpretation, \" effect size between \", sep = \"\"))\n\n# Another example that will fail\n# Source: https://github.com/wmlutz/r-lesson/blob/master/hold_temp.R\naw$cert_Admin \u003c- ifelse(\"Admin\" %in% raw[,\"Certifications\"], 1,0)\n```\n\n\n## Examples\n\nI did a quick advanced GitHub search of `ifelse language:R` to find these\nexamples. I've tried to pick a mix of short to long examples. I also wanted to\nmake sure this function works on code in the \"wild\".\n\n\n### Simple\n\nSource: https://github.com/Anis-Bensaid/SBT13/blob/master/Script%20R%20Final.R\n\n**Before**:\n\n```r\ndata$a1 \u003c- ifelse(bd1$A1==\"Pas du tout\", 0, ifelse(bd1$A1==\"Un peu\", 1,ifelse(bd1$A1== \"Beaucoup\", 2,ifelse(bd1$A1 == \"Enormément\", 3,NA))))\n```\n\n**After**:\n\n```r\ndata$a1 \u003c- case_when(\n  bd1$A1==\"Pas du tout\" ~  0,\n   bd1$A1==\"Un peu\" ~  1,\n  bd1$A1== \"Beaucoup\" ~  2,\n  bd1$A1 == \"Enormément\" ~  3,\n  TRUE ~ NA\n)\n```\n\n\n### Mix of numbers\n\nSource: https://github.com/ajpelu/qpyr_resilience/blob/master/R/getComposite.R\n\n**Before**:\n\n```r\ngetComposite_nonleap \u003c- function(m,d){\n  # m = month\n  # d = day\n  ifelse(m == 1 \u0026 d \u003c 17, 1,\n  ifelse(m == 1 \u0026 d \u003e= 17, 2,\n  ifelse(m == 2 \u0026 d \u003c 2, 2,\n  ifelse(m == 2 \u0026 d \u003c 18, 3,\n  ifelse(m == 2 \u0026 d \u003e= 18, 4,\n  ifelse(m == 3 \u0026 d \u003c 6, 4,\n  ifelse(m == 3 \u0026 d \u003c 22, 5,\n  ifelse(m == 3 \u0026 d \u003e= 22, 6,\n  ifelse(m == 4 \u0026 d \u003c 7, 6,\n  ifelse(m == 4 \u0026 d \u003c 23, 7,\n  ifelse(m == 4 \u0026 d \u003e= 23, 8,\n  ifelse(m == 5 \u0026 d \u003c 9, 8,\n  ifelse(m == 5 \u0026 d \u003c 25, 9,\n  ifelse(m == 5 \u0026 d \u003e= 25, 10,\n  ifelse(m == 6 \u0026 d \u003c 10, 10,\n  ifelse(m == 6 \u0026 d \u003c 26, 11,\n  ifelse(m == 6 \u0026 d \u003e= 26, 12,\n  ifelse(m == 7 \u0026 d \u003c 12, 12,\n  ifelse(m == 7 \u0026 d \u003c 28, 13,\n  ifelse(m == 7 \u0026 d \u003e= 28, 14,\n  ifelse(m == 8 \u0026 d \u003c 13, 14,\n  ifelse(m == 8 \u0026 d \u003c 29, 15,\n  ifelse(m == 8 \u0026 d \u003e= 29, 16,\n  ifelse(m == 9 \u0026 d \u003c 14, 16,\n  ifelse(m == 9 \u0026 d \u003c 30, 17,\n  ifelse(m == 9 \u0026 d \u003e= 30, 18,\n  ifelse(m == 10 \u0026 d \u003c 16, 18,\n  ifelse(m == 10 \u0026 d \u003e= 16, 19,\n  ifelse(m == 11 \u0026 d \u003c 17, 20,\n  ifelse(m == 11 \u0026 d \u003e= 17, 21,\n  ifelse(m == 12 \u0026 d \u003c 3, 21,\n  ifelse(m == 12 \u0026 d \u003c 19, 22, 23))))))))))))))))))))))))))))))))\n}\n```\n\n**After**:\n\n```r\ngetComposite_nonleap \u003c- function(m,d){\n  # m = month \n  # d = day \n  case_when(\n  m == 1 \u0026 d \u003c 17 ~ 1,\n  m == 1 \u0026 d \u003e= 17 ~ 2,\n  m == 2 \u0026 d \u003c 2 ~ 2,\n  m == 2 \u0026 d \u003c 18 ~ 3,\n  m == 2 \u0026 d \u003e= 18 ~ 4,\n  m == 3 \u0026 d \u003c 6 ~ 4,\n  m == 3 \u0026 d \u003c 22 ~ 5,\n  m == 3 \u0026 d \u003e= 22 ~ 6,\n  m == 4 \u0026 d \u003c 7 ~ 6,\n  m == 4 \u0026 d \u003c 23 ~ 7,\n  m == 4 \u0026 d \u003e= 23 ~ 8,\n  m == 5 \u0026 d \u003c 9 ~ 8,\n  m == 5 \u0026 d \u003c 25 ~ 9,\n  m == 5 \u0026 d \u003e= 25 ~ 10,\n  m == 6 \u0026 d \u003c 10 ~ 10,\n  m == 6 \u0026 d \u003c 26 ~ 11,\n  m == 6 \u0026 d \u003e= 26 ~ 12,\n  m == 7 \u0026 d \u003c 12 ~ 12,\n  m == 7 \u0026 d \u003c 28 ~ 13,\n  m == 7 \u0026 d \u003e= 28 ~ 14,\n  m == 8 \u0026 d \u003c 13 ~ 14,\n  m == 8 \u0026 d \u003c 29 ~ 15,\n  m == 8 \u0026 d \u003e= 29 ~ 16,\n  m == 9 \u0026 d \u003c 14 ~ 16,\n  m == 9 \u0026 d \u003c 30 ~ 17,\n  m == 9 \u0026 d \u003e= 30 ~ 18,\n  m == 10 \u0026 d \u003c 16 ~ 18,\n  m == 10 \u0026 d \u003e= 16 ~ 19,\n  m == 11 \u0026 d \u003c 17 ~ 20,\n  m == 11 \u0026 d \u003e= 17 ~ 21,\n  m == 12 \u0026 d \u003c 3 ~ 21,\n  m == 12 \u0026 d \u003c 19 ~ 22,\n  TRUE ~ 23\n)\n}\n```\n\n\n### Text and numbers\n\nSource: https://github.com/WWF-ConsEvidence/ACES/blob/master/Mozambique/Harmonized_Demographic_Data.R\n\n**Before**:\n\n```r\nCommunity2008 \u003c- ifelse(dat2008$Community_code == 241601, \"Fuzi\", \n             ifelse(dat2008$Community_code == 41602, \"Moebase\",\n             ifelse(dat2008$Community_code==41603, \"Txotxo\",\n             ifelse(dat2008$Community_code ==41604, \"Monudo/Murudo\", \n             ifelse(dat2008$Community_code ==41605, \"Namige\", \n             ifelse(dat2008$Community_code ==41606, \"Solulo\", \n             ifelse(dat2008$Community_code ==41607, \"Magene\", \n             ifelse(dat2008$Community_code ==41608, \"Murateia\", \n             ifelse(dat2008$Community_code ==41609, \"Namaquete\", \n             ifelse(dat2008$Community_code ==31101, \"Mtopa\", \n             ifelse(dat2008$Community_code ==31102, \"Hori\", \n             ifelse(dat2008$Community_code ==31103, \"Mecane (Oua)\", \n             ifelse(dat2008$Community_code ==31104, \"Nacocolo\", \n             ifelse(dat2008$Community_code ==31105, \"Pacone\", \n             ifelse(dat2008$Community_code ==31106, \"Ampuitine\",\n             ifelse(dat2008$Community_code ==31107, \"Corane\",\n             ifelse(dat2008$Community_code ==31108, \"Lalaue\",\n             ifelse(dat2008$Community_code ==31109, \"Mavele\",\n             ifelse(dat2008$Community_code ==31110, \"Mucoroma\",\n             ifelse(dat2008$Community_code ==31111, \"Nacalela\",\n             ifelse(dat2008$Community_code ==31112, \"Nambui\",\n             ifelse(dat2008$Community_code ==31113, \"Namichir\",\n             ifelse(dat2008$Community_code ==31114, \"Nanheua\",\n             ifelse(dat2008$Community_code ==31115, \"Natere\",\n             ifelse(dat2008$Community_code ==31116, \"Tipane\",\n             ifelse(dat2008$Community_code ==30101, \"Iarupa\",\n             ifelse(dat2008$Community_code ==30102, \"Mituco\",\n             ifelse(dat2008$Community_code ==30103, \"Macogone\",\n             ifelse(dat2008$Community_code ==30104, \"Munar\",\n             ifelse(dat2008$Community_code ==30105, \"Namiepe\",\n             ifelse(dat2008$Community_code ==30106, \"Nauluco\",\n             ifelse(dat2008$Community_code ==30107, \"Pulizica\",\n             ifelse(dat2008$Community_code ==30108, \"Naheco\",\n             ifelse(dat2008$Community_code ==30109, \"Namame\",NA))))))))))))))))))))))))))))))))))\n```\n\n**After**:\n\n```r\nCommunity2008 \u003c- case_when(\n  dat2008$Community_code == 241601 ~ \"Fuzi\",\n  dat2008$Community_code == 41602 ~ \"Moebase\",\n  dat2008$Community_code==41603 ~ \"Txotxo\",\n  dat2008$Community_code ==41604 ~ \"Monudo/Murudo\",\n  dat2008$Community_code ==41605 ~ \"Namige\",\n  dat2008$Community_code ==41606 ~ \"Solulo\",\n  dat2008$Community_code ==41607 ~ \"Magene\",\n  dat2008$Community_code ==41608 ~ \"Murateia\",\n  dat2008$Community_code ==41609 ~ \"Namaquete\",\n  dat2008$Community_code ==31101 ~ \"Mtopa\",\n  dat2008$Community_code ==31102 ~ \"Hori\",\n  dat2008$Community_code ==31103 ~ \"Mecane (Oua)\",\n  dat2008$Community_code ==31104 ~ \"Nacocolo\",\n  dat2008$Community_code ==31105 ~ \"Pacone\",\n  dat2008$Community_code ==31106 ~ \"Ampuitine\",\n  dat2008$Community_code ==31107 ~ \"Corane\",\n  dat2008$Community_code ==31108 ~ \"Lalaue\",\n  dat2008$Community_code ==31109 ~ \"Mavele\",\n  dat2008$Community_code ==31110 ~ \"Mucoroma\",\n  dat2008$Community_code ==31111 ~ \"Nacalela\",\n  dat2008$Community_code ==31112 ~ \"Nambui\",\n  dat2008$Community_code ==31113 ~ \"Namichir\",\n  dat2008$Community_code ==31114 ~ \"Nanheua\",\n  dat2008$Community_code ==31115 ~ \"Natere\",\n  dat2008$Community_code ==31116 ~ \"Tipane\",\n  dat2008$Community_code ==30101 ~ \"Iarupa\",\n  dat2008$Community_code ==30102 ~ \"Mituco\",\n  dat2008$Community_code ==30103 ~ \"Macogone\",\n  dat2008$Community_code ==30104 ~ \"Munar\",\n  dat2008$Community_code ==30105 ~ \"Namiepe\",\n  dat2008$Community_code ==30106 ~ \"Nauluco\",\n  dat2008$Community_code ==30107 ~ \"Pulizica\",\n  dat2008$Community_code ==30108 ~ \"Naheco\",\n  dat2008$Community_code ==30109 ~ \"Namame\",\n  TRUE ~ NA\n)\n```\n\n\n## TODO\n\n- Convert multiple `ifelse` statements that span multiple lines\n  (e.g., https://github.com/Anis-Bensaid/SBT13/blob/master/Script%20R%20Final.R)\n\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferictleung%2Funnestifelse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferictleung%2Funnestifelse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferictleung%2Funnestifelse/lists"}