[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-0-en-105":3,"doc-seo-267624-105":59,"doc-detail-267624-en":130},{"code":4,"msg":5,"data":6},0,"success",[7,13,18,23,28,33,38,43,48,51,55],{"id":8,"doc_module":4,"doc_module_name":9,"category_name":10,"show_sort_weight":11,"slug":12},1,"Document","Story & Novel",90,"story-novel",{"id":14,"doc_module":4,"doc_module_name":9,"category_name":15,"show_sort_weight":16,"slug":17},2,"Literature",80,"literature",{"id":19,"doc_module":4,"doc_module_name":9,"category_name":20,"show_sort_weight":21,"slug":22},4,"Exam",70,"exam",{"id":24,"doc_module":4,"doc_module_name":9,"category_name":25,"show_sort_weight":26,"slug":27},5,"Comic",60,"comic",{"id":29,"doc_module":4,"doc_module_name":9,"category_name":30,"show_sort_weight":31,"slug":32},6,"Technology",50,"technology",{"id":34,"doc_module":4,"doc_module_name":9,"category_name":35,"show_sort_weight":36,"slug":37},7,"Healthcare",40,"healthcare",{"id":39,"doc_module":4,"doc_module_name":9,"category_name":40,"show_sort_weight":41,"slug":42},8,"Research & Report",30,"research-report",{"id":44,"doc_module":4,"doc_module_name":9,"category_name":45,"show_sort_weight":46,"slug":47},9,"Religion & Spirituality",20,"religion-spirituality",{"id":46,"doc_module":4,"doc_module_name":9,"category_name":49,"show_sort_weight":46,"slug":50},"World Cup","world-cup",{"id":52,"doc_module":4,"doc_module_name":9,"category_name":53,"show_sort_weight":52,"slug":54},10,"Lifestyle","lifestyle",{"id":56,"doc_module":4,"doc_module_name":9,"category_name":57,"show_sort_weight":24,"slug":58},19,"General","general",{"code":4,"msg":60,"data":61},"ok",{"site_id":62,"language":63,"slug":64,"title":65,"keywords":66,"description":67,"schema_data":68,"social_meta":123,"head_meta":125,"extra_data":127,"updated_unix":129},105,"en","lesson-3-control-flow-with-if-and-ifelse","Lesson 3 - Control flow with if and ifelse","","Lesson 3 explains conditional logic in R using if and ifelse. It walks through how to predict outputs for TRUE/FALSE and NA-related conditions, then demonstrates common patterns for ifelse in transforming variables. The lesson applies these ideas to opportunity insights data by adjusting tier_name values, collapsing categories, and handling missing SAT scores via mean imputation and indicator columns, supporting both reasoning and practical data-cleaning workflows.",{"@graph":69,"@context":122},[70,84,105],{"@type":71,"itemListElement":72},"BreadcrumbList",[73,77,79,82],{"item":74,"name":75,"@type":76,"position":8},"https://docshare.wps.com","Home","ListItem",{"item":78,"name":9,"@type":76,"position":14},"https://docshare.wps.com/document/",{"item":80,"name":30,"@type":76,"position":81},"https://docshare.wps.com/document/technology/",3,{"item":83,"name":65,"@type":76,"position":19},"https://docshare.wps.com/document/lesson-3-control-flow-with-if-and-ifelse/267624/",{"url":83,"name":65,"@type":85,"image":86,"author":91,"headline":65,"publisher":94,"fileFormat":97,"inLanguage":63,"description":67,"dateModified":98,"datePublished":99,"encodingFormat":97,"isAccessibleForFree":100,"interactionStatistic":101},"DigitalDocument",{"url":87,"@type":88,"width":89,"height":90},"https://docshare.wps.com/thumbnails/lesson-3-control-flow-with-if-and-ifelse/267624.png","ImageObject",300,407,{"name":92,"@type":93},"Xiajie","Person",{"url":74,"name":95,"@type":96},"DocShare","Organization","application/pdf","2026-10-08","2026-09-14",true,{"@type":102,"interactionType":103,"userInteractionCount":52},"InteractionCounter",{"@type":104},"ViewAction",{"@type":106,"mainEntity":107},"FAQPage",[108,114,118],{"name":109,"@type":110,"acceptedAnswer":111},"What is the key difference between using if and ifelse in R?","Question",{"text":112,"@type":113},"ifelse evaluates a condition element-wise and returns different values based on the condition. The lesson focuses on ifelse for transforming vectors/columns during data cleaning and variable recoding.","Answer",{"name":115,"@type":110,"acceptedAnswer":116},"How do NA values behave in conditional expressions with logical operators?",{"text":117,"@type":113},"The warm-up section highlights cases where NA can affect the result differently than TRUE/FALSE contagion. Running the provided code confirms the expected output for NA-related logic.",{"name":119,"@type":110,"acceptedAnswer":120},"How can ifelse help handle missing SAT scores without dropping rows?",{"text":121,"@type":113},"The lesson suggests using mutate() with ifelse() to replace NA in sat_avg_2013 using the mean of available scores, and creating an indicator column missing_sat_avg_2013 to mark missingness.","https://schema.org",{"og:url":83,"og:type":124,"og:title":65,"og:site_name":95,"og:description":67},"article",{"robots":126,"canonical":83},"index,follow",{"doc_id":128,"site_id":62},267624,1789423254,{"code":4,"msg":5,"data":131},{"doc_id":128,"user_id":132,"nickname":92,"user_avatar":133,"doc_module":4,"category_id":29,"category_name":30,"doc_title":65,"doc_description":67,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":52,"is_deleted":4,"is_public":8,"is_downloadable":8,"audit_status":8,"page_count":29,"language":139,"language_code":63,"site_id":62,"html_lang":63,"table_of_contents":140,"faqs":141,"seo_title":142,"seo_description":67,"update_tm":129,"read_time":143},8814010472675,"https://avatar.qwps.com/avatar/WGlhamll","Lesson 3: Control ﬂow with if and ifelse  \nAri Anisfeld  \n8/31/2021  \nWe expect you to watch the class 3 material, here prior to lab. Download the data before lab.  \nData:  \nFor this lab, we will use data from Opportunity Insights: [https://opportunityinsights.org/data/](https://opportunityinsights.org/data/)  \n[1](1). Download the Stata ﬁle and Readme for “College Level Characteristics from the IPEDS Database and the College Scorecard”.  \nRmd’s and working directories (To be covered by TAs during lab)  \n1. Rmds keep track of their own working directories. Try getwd() in a code chunk in your Rmd and then run it again in the console. You should notice that R in the console uses your default working directory. On the other hand, the lab_3 .Rmd knows which folder it is in and uses that folder as the working directory!  \n2. If you didn’t already, put the opportunity insights data you downloaded in your preferred data location. If you followed the set-up from above, you should be able to read the data in your Rmd with no error. We provide three options dependening on how you have structured your folders. Option 1 is the easiest for a ﬁrst time user. Keep the data in the same folder as the Rmd. Option 2 and 3 are what you’re more likely to  \nsee in a professional environment. 1  \nlibrary (haven)  \n\\# Option 1:  \n\\# data is in the same folder as Rmd \\#  \n\\# project_folder/  \n\\# - lab_ 3.Rmd  \n\\# - mrc_ table10 . dta  \nmrc_data \u003C -read_dta ( \"mrc_table10.dta\" )  \n\\# Option 2:  \n\\# data is in a data folder inside the folder with the Rmd \\#  \n\\# project_folder/  \n\\# - lab_ 3.Rmd  \n\\# - data/  \n\\# - mrc_ table10 . dta  \n1We did a poll among the TAs about their preferred directory structure. We were split between Option 2, Option 3, and a feeling like the choice was “Too personal” to make a cohort wide  \nmrc_data \u003C -read_dta (\"data/mrc_table10.dta\" )  \n\\# Option 3:  \n\\# data is in a data folder that is at the same level  \n\\# as the folder with the Rmd .  \n\\#  \n\\# project_folder/  \n\\# - data/  \n\\# - mrc_ table10 . dta  \n\\# - code/  \n\\# - lab_ 3.Rmd  \nmrc_data \u003C -read_dta ( \" . ./data/mrc_table10.dta\" )  \nWarm-up: Conditional statements and ifelse:  \n1. Without running the code, predict what the output will be. Then, see if you were right by running the code in the console.  \nTrue or False  \na. TRUE | FALSE  \nb. TRUE | (FALSE & FALSE)  \nc. TRUE | (10 \u003C 4)  \nd. TRUE | (10 \u003C 4 & )  \ne. TRUE | (4 > pi & 3 \u003C pi & exp(1) >= 3 & 1e6 \u003C 2ˆ30)  \nf. 4 > 2 | 2 > 4  \ng. What rule do these problems demonstrate?  \nTrue and False  \na. TRUE & FALSE  \nb. TRUE & (FALSE & FALSE)  \nc. TRUE & (10 \u003C 4)  \nd. TRUE & (10 \u003C 4 & )  \ne. TRUE & (4 > pi & 3 \u003C pi & exp(1) >= 3 & 1e6 \u003C 2ˆ30)  \nf. 4 > 2 & 2 > 4  \ng. What rule do these problems demonstrate?  \nTrue and NA  \nh. There are a few times when NA are not contagious. Run the code and think about how this relates to your ﬁndings above.  \nTRUE & NA FALSE & NA TRUE | NA FALSE | NA  \n1. Without running the code, predict what the output will be. Then, see if you were right by running the code in the console.  \nifelse (TRUE , \"yes\" , \"no\" ) ifelse (FALSE , \"yes\" , \"no\" )  \nifelse (c(TRUE , FALSE , TRUE , FALSE ), \"yes\" , \"no\" ) ifelse (c(TRUE & FALSE ,  \nFALSE | TRUE ,  \nTRUE | TRUE ,  \nFALSE & FALSE ),  \n\"yes\" , \"no\" )  \nifelse (c(NA , TRUE , FALSE ), \"yes\" , \"no\" )  \nifelse (c(NA , NA , TRUE , FALSE ), \"yes\" , \"no\" )  \nCommon uses of ifelse  \n1. Run the following code and you will see the distinct tier_names available in the dataset.  \nmrc_data %>% distinct (tier_name)  \na. ifelse can be used to adjust entries in the tier_name column. Change “Two-year (public and private not-for-proﬁt)” to “Two-year (public and private)”.2  \n\\# Fill in the . . . with the appropriate code  \nmrc_data %>%  \nmutate (tier_name = ifelse ( . . . , . . . , tier_name))  \na. ifelse is often used to collapse tiers. Redeﬁne tier_name so that “Nonselective four-year public”and “Nonselective four-year private not-for-proﬁt” are grouped together as “N","cbCaialimhKo5QfU","https://ap.wps.com/l/cbCaialimhKo5QfU","pdf",334183,"English","# Warm-up: Conditional statements and ifelse\n## True/False and NA behavior\n# Common uses of ifelse\n## Adjusting tier_name entries\n## Collapsing tiers\n## Replacing NA and tracking missingness\n# Extension: College choice","[{\"question\":\"What is the key difference between using if and ifelse in R?\",\"answer\":\"ifelse evaluates a condition element-wise and returns different values based on the condition. The lesson focuses on ifelse for transforming vectors/columns during data cleaning and variable recoding.\"},{\"question\":\"How do NA values behave in conditional expressions with logical operators?\",\"answer\":\"The warm-up section highlights cases where NA can affect the result differently than TRUE/FALSE contagion. Running the provided code confirms the expected output for NA-related logic.\"},{\"question\":\"How can ifelse help handle missing SAT scores without dropping rows?\",\"answer\":\"The lesson suggests using mutate() with ifelse() to replace NA in sat_avg_2013 using the mean of available scores, and creating an indicator column missing_sat_avg_2013 to mark missingness.\"}]","Lesson 3 - Control flow with if and ifelse | PDF",15]