

I’m including it here for completeness, however beginners can feel free to skip down to the summarize() section and return to case_when() later.Īt times we want to create a label column that tests multiple conditions. This function is useful but quite involved. One option to check the new labels is to plot the price column as a histogram, and fill the bars according to price_label: All the rows we can see are price < 5000 and labelled ‘cheap’. It seems that the ifelse() function has worked. Remember that we need two closing brackets, one for the mutate() function, and one for the ifelse() inside it. # carat cut color clarity depth table price price_label How would you filter for all vehicles with automatic transmission?ĭiamond_df %>% select( -x, -y, -z) %>% mutate( price_label = ifelse(price > 5000, 'expensive', 'cheap')) # A tibble: 53,940 x 8

Note that the letter order and case have to be matched exactly. # 10 dodge dakota pickup 4wd 4.7 2008 8 auto(l5) 4 14 19 r pickup # 9 dodge dakota pickup 4wd 4.7 2008 8 auto(l5) 4 14 19 r pickup # 8 dodge dakota pickup 4wd 3.9 1999 6 manual(m5) 4 14 17 r pickup # 7 dodge dakota pickup 4wd 3.9 1999 6 auto(l4) 4 13 17 r pickup # 6 dodge dakota pickup 4wd 3.7 2008 6 auto(l4) 4 14 18 r pickup # 5 dodge dakota pickup 4wd 3.7 2008 6 manual(m6) 4 15 19 r pickup Mpg_df %>% filter( str_detect(model, '4wd')) # A tibble: 74 x 11 If you are working in an R text document (.R format) or directly in the console, after running this command you will see the dimensions of the output data frame printed in grey text above the column names.Īlternatively you can ‘send’ the output of filter (a data frame) into the dim() function. Here we are ‘sending’ the mpg_df data frame into the function filter(), which tests each value in the year column for the number 1999, and returns those rows where the filter() condition is TRUE. # manufacturer model displ year cyl trans drv cty hwy fl class
