You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Perhaps we want values that are O or Other to actually be NA, then `case_when` can be helpful for this. We could specify everything else and drop `.default = Treatment` or we could specify NA directly with `NA_character_`
-`case_when()` requires `mutate()` when working with dataframes/tibbles
796
-
-`case_when()` can recode **entire values** based on **conditions** (need quotes for conditions and new values)
797
-
- remember `case_when()` needs `TRUE ~ varaible` to keep values that aren't specified by conditions, otherwise will be `NA`
798
-
799
-
**Note:** you might see the `recode()` function, it only does some of what `case_when()` can do, so we skipped it, but it is in the extra slides at the end.
"Artwork by @allison_horst". https://allisonhorst.com/
807
-
808
-
809
-
## Summary Continued
810
-
811
-
-`stringr` package has great functions for looking for specific **parts of values** especially `filter()` and `str_detect()` combined
812
-
-`stringr` also has other useful string functions like `str_detect()` (finding patterns in a column or vector), `str_subset()` (parsing text), `str_replace()` (replacing the first instance in values), `str_replace_all()` (replacing all instances in each value) and **more**!
813
-
-`separate()` can split columns into additional columns
814
-
-`unite()` can combine columns
815
-
-`:` can indicate when you want to start and end with columns next to one another
Image by <ahref="https://pixabay.com/users/geralt-9301/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=812226">Gerd Altmann</a> from <ahref="https://pixabay.com//?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=812226">Pixabay</a>
833
-
834
-
# Extra Slides
835
-
836
-
## `recode()` function
837
-
838
-
This is similar to `case_when()` but it can't do as much.
Copy file name to clipboardExpand all lines: modules/Data_Cleaning/lab/Data_Cleaning_Lab_Key.Rmd
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -148,7 +148,7 @@ NEW_TIBBLE <- OLD_TIBBLE %>%
148
148
mutate(NEW_COLUMN = case_when(
149
149
OLD_COLUMN %in% c( ... ) ~ ... ,
150
150
OLD_COLUMN %in% c( ... ) ~ ... ,
151
-
TRUE ~ OLD_COLUMN
151
+
.default = OLD_COLUMN
152
152
))
153
153
```
154
154
@@ -158,7 +158,7 @@ BloodType <- BloodType %>%
158
158
mutate(exposure = case_when(
159
159
exposure %in% c("N", "n", "No", "no") ~ "No",
160
160
exposure %in% c("Y", "y", "Yes", "yes") ~ "Yes",
161
-
TRUE ~ exposure # the only other value is an NA so we could include this or we don't need to (it's generally good practice unless we want to create NAs)
161
+
.default = exposure # the only other value is an NA so we could include this or we don't need to (it's generally good practice unless we want to create NAs)
0 commit comments