@@ -271,6 +271,21 @@ case. Fill the respective columns.
271271
272272</div >
273273
274+ <div class =" answer " >
275+
276+ The following code will iterate through ` possible_columns ` and replace
277+ the pattern ` grepl() ` is looking for with each option. It will test for
278+ that option, and save the results in the corresponding column.
279+
280+ for(option in possible_columns){
281+
282+ # fill dataframe iterativly.
283+ pet_output[ , option] = grepl(option, pet_vector, ignore.case = TRUE)
284+
285+ }
286+
287+ </div >
288+
274289### Remove the Known Options to Find “Other”
275290
276291Once we have our “knowns” taken care of, we can work on the others. The
@@ -286,6 +301,29 @@ remaining values to the “other” column of `pet_output`.
286301
287302</div >
288303
304+ <div class =" answer " >
305+
306+ The following will remove all known possibilities, clean the remainder,
307+ and assign it to the ‘other’ column.
308+
309+ for(option in possible_columns){
310+
311+ # remove all known options
312+ pet_vector = gsub(pattern = option, pet_vector, replacement = '', ignore.case = TRUE)
313+
314+ }
315+
316+ # clear commas and whitespace
317+ pet_vector = gsub(pattern = ',', pet_vector, replacement = '', ignore.case = TRUE)
318+ pet_vector = trimws(pet_vector)
319+
320+ # Fill in 'other'
321+ pet_output$other = pet_vector
322+ # Turn blanks into NAs
323+ pet_output[pet_output$other == '', 'other'] = NA
324+
325+ </div >
326+
289327### Turn it Back into a Function
290328
291329If we look at our code all together now, it looks like the following. If
@@ -347,6 +385,57 @@ Convert our code back into a function, call the function
347385
348386</div >
349387
388+ <div class =" answer " >
389+
390+ comma_split = function(vector_to_split, possible_columns){
391+
392+ # make a base dataframe with rows for each of our cases.
393+ output = data.frame(
394+ "id" = 1:length(vector_to_split)
395+ )
396+
397+ # iterate through all options and create a column with NAs for it
398+ for(option in possible_columns){
399+
400+ # make a new column with a character version of each possible option.
401+ output[, as.character(option)] = NA
402+
403+ }
404+
405+ # fill output df
406+ for(option in possible_columns){
407+
408+ # fill dataframe iterativly.
409+ output[ , option] = grepl(option, vector_to_split, ignore.case = TRUE)
410+
411+ }
412+
413+ # clear all know options
414+ for(option in possible_columns){
415+
416+ # remove all known options
417+ vector_to_split = gsub(pattern = option, vector_to_split, replacement = "", ignore.case = TRUE)
418+
419+ }
420+
421+ # clear commas and whitespace
422+ vector_to_split = gsub(pattern = ",", vector_to_split, replacement = "", ignore.case = TRUE)
423+ vector_to_split = trimws(vector_to_split)
424+
425+ # Fill in "other"
426+ output$other = vector_to_split
427+ # Turn blanks into NAs
428+ output[output$other == "" & !is.na(output$other), "other"] = NA
429+
430+ # return output
431+ return(output)
432+ }
433+
434+ comma_split(vector_to_split = survey$pets,
435+ possible_columns = c("dog", "cat", "fish", "bird", "reptile", "rock", "none"))
436+
437+ </div >
438+
350439Once you have the function created, try it on another column! Your
351440output should match mine.
352441
0 commit comments