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
@@ -86,7 +86,7 @@ Now, that is cool, but we can make it better. Specifically, if we look at our su
86
86
87
87
## Break it Down
88
88
89
-
Our first step is going to be writing the code to accomplish what we want, then we can package it as a function. I've gutted our `pet_split()` function below. We will be starting from that base and working to make it so that we never explicitly call for anything in our code. For example, instead of coding all of the possibilities of pet (dog, cat, fish, bird, reptile, rock, none) inside the function itself, we want to write our code such that it can accept *any* list of possibilities as an argument and work from that.
89
+
Our first step is going to be writing the code to accomplish what we want, then we can package it as a function. I've gutted our `pet_split()` function below. We will be starting from that base and working to make it so that we never call for anything specific to pets in our code. For example, instead of coding all of the possibilities of pet (dog, cat, fish, bird, reptile, rock, none) inside the function itself, we want to write our code such that it can accept *any* list of possibilities as an argument and work from that.
90
90
91
91
```{r}
92
92
# set up a psudo argument
@@ -148,7 +148,7 @@ pet_output = data.frame(
148
148
"id" = 1:length(pet_vector)
149
149
)
150
150
151
-
# iterate through all optinos and create a column with NAs for it
151
+
# iterate through all options and create a column with NAs for it
152
152
for(option in possible_columns){
153
153
154
154
# make a new column with a character version of each possible option.
@@ -159,12 +159,16 @@ for(option in possible_columns){
159
159
160
160
If we look at out output now, it is *exactly* the same as if we made each column ourselves, but now it is done by providing a vector of options. *And we can change those options to whatever we want*. This will come in handy later.
161
161
162
+
```{r}
163
+
pet_output
164
+
```
165
+
162
166
### Test for Each Option
163
167
164
-
Our next step, as before, is to test for each possible option and fill in the respective columns. We will use iteration here as well.
168
+
Our next stepis to test for each possible option (in this case types of pets) and fill in the respective columns. We will use iteration here as well.
165
169
166
170
::: {.question}
167
-
Using the same principle as above, iterate over each option in `possible_columns` and use `grepl()` to test if the pet appeared in that case. Fill the respective columns.*Make sure that `pet_vector` and `possible_columns` are reset to normal before you try!*
171
+
Using the same principle as above, iterate over each option in `possible_columns` and use `grepl()` to test if the pet appeared in that case. Fill the respective columns.
0 commit comments