Search code examples
rdplyrtidyversestartswithends-with

dplyr::starts_with and ends_with not subsetting based on arguments


I want to select a number of variables based on thier names to transform them. The variable names all start with inq and end with 7, 8, 10, 13:15. This is not working for me... Apologies if this is obvious, but I cannot get it to work. Am I using the wrong functions, putting my functions and arguments together wrong, or something else?

A reproducible example:

structure(list(inq1_1 = c(NA, 7, 5, 1, 1, 6, 5, 2, NA, NA), inq1_2 = c(NA, 
7, 5, 1, 1, 6, 5, 5, NA, NA), inq1_3 = c(NA, 6, 4, 2, 1, 5, 2, 
1, NA, NA), inq1_4 = c(NA, 6, 6, 1, 1, 6, 5, 1, NA, NA), inq1_5 = c(NA, 
7, 3, 1, 1, 6, 2, 1, NA, NA), inq1_6 = c(NA, 7, 4, 4, 2, 7, 2, 
4, NA, NA), inq1_7 = c(NA, 2, 4, 6, 7, 3, 1, 7, NA, NA), inq1_8 = c(NA, 
1, NA, 2, 7, 2, 1, 4, NA, NA), inq1_9 = c(NA, 4, 6, 3, 1, 3, 
7, 1, NA, NA), inq1_10 = c(NA, 3, 5, 7, 4, 4, 2, 7, NA, NA), 
    inq1_11 = c(NA, 5, 4, 7, 1, 6, 7, 6, NA, NA), inq1_12 = c(NA, 
    7, 5, 7, 4, 6, 7, 2, NA, NA), inq1_13 = c(NA, 3, 4, 6, 4, 
    3, 4, 4, NA, NA), inq1_14 = c(NA, 3, 2, 4, 4, 2, 1, 4, NA, 
    NA), inq1_15 = c(NA, 2, 2, 3, 5, 2, 4, 4, NA, NA), inqfinal_1 = c(5, 
    NA, 3, NA, NA, NA, NA, NA, NA, NA), inqfinal_2 = c(5, NA, 
    3, NA, NA, NA, NA, NA, NA, NA), inqfinal_3 = c(6, NA, 3, 
    NA, NA, NA, NA, NA, NA, NA), inqfinal_4 = c(5, NA, 3, NA, 
    NA, NA, NA, NA, NA, NA), inqfinal_5 = c(5, NA, 3, NA, NA, 
    NA, NA, NA, NA, NA), inqfinal_6 = c(6, NA, 3, NA, NA, NA, 
    NA, NA, NA, NA), inqfinal_7 = c(4, NA, 3, NA, NA, NA, NA, 
    NA, NA, NA), inqfinal_8 = c(2, NA, 3, NA, NA, NA, NA, NA, 
    NA, NA), inqfinal_9 = c(5, NA, 3, NA, NA, NA, NA, NA, NA, 
    NA), inqfinal_10 = c(4, NA, 3, NA, NA, NA, NA, NA, NA, NA
    ), inqfinal_11 = c(6, NA, 4, NA, NA, NA, NA, NA, NA, NA), 
    inqfinal_12 = c(6, NA, 4, NA, NA, NA, NA, NA, NA, NA), inqfinal_13 = c(4, 
    NA, 3, NA, NA, NA, NA, NA, NA, NA), inqfinal_14 = c(2, NA, 
    2, NA, NA, NA, NA, NA, NA, NA), inqfinal_15 = c(2, NA, 2, 
    NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

I am trying to become tidy and utilising dplyr as per the code below:

# select specific columns
sf_df %>% select(starts_with("inq"),
                 ends_with(7, 8, 10, 13:15)) %>% view(title = "test")

Alas, I get the following error:

Error in ends_with(7, 8, 10, 13:15) : unused argument (13:15)
14. .f(.x[[i]], ...)
13. map(.x[sel], .f, ...)
12. map_if(ind_list, is_helper, eval_tidy)
11. vars_select_eval(.vars, quos)
10. tidyselect::vars_select(names(.data), !!!quos(...))
9. select.data.frame(., starts_with("inq"), ends_with(7, 8, 10, 13:15))
8. select(., starts_with("inq"), ends_with(7, 8, 10, 13:15))
7. function_list[[i]](value)
6. freduce(value, `_function_list`)
5. `_fseq`(`_lhs`)
4. eval(quote(`_fseq`(`_lhs`)), env, env)
3. eval(quote(`_fseq`(`_lhs`)), env, env)
2. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
1. sf_df %>% select(starts_with("inq"), ends_with(7, 8, 10, 13:15)) %>% view(title = "test")

Any help would be greatly appreciated! Thank you in advance.

Cheers, Atanas.


Solution

  • A better option would be matches to match a regex pattern in the column name. Here, it matches the pattern 'ing' at the beginning (^) of the column name and numbers at the end ($) of the column name

    sf_df %>% 
        select(matches('^inq.*(7|8|10|13|14|15)$'))
    # A tibble: 10 x 12
    #   inq1_7 inq1_8 inq1_10 inq1_13 inq1_14 inq1_15 inqfinal_7 inqfinal_8 inqfinal_10 inqfinal_13 inqfinal_14 inqfinal_15
    #    <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>      <dbl>      <dbl>       <dbl>       <dbl>       <dbl>       <dbl>
    # 1     NA     NA      NA      NA      NA      NA          4          2           4           4           2           2
    # 2      2      1       3       3       3       2         NA         NA          NA          NA          NA          NA
    # 3      4     NA       5       4       2       2          3          3           3           3           2           2
    # 4      6      2       7       6       4       3         NA         NA          NA          NA          NA          NA
    # 5      7      7       4       4       4       5         NA         NA          NA          NA          NA          NA
    # 6      3      2       4       3       2       2         NA         NA          NA          NA          NA          NA
    # 7      1      1       2       4       1       4         NA         NA          NA          NA          NA          NA
    # 8      7      4       7       4       4       4         NA         NA          NA          NA          NA          NA
    # 9     NA     NA      NA      NA      NA      NA         NA         NA          NA          NA          NA          NA
    #10     NA     NA      NA      NA      NA      NA         NA         NA          NA          NA          NA          NA
    

    Note that by using both starts_with and ends_with, the desired result may not be the expected one. The OP's dataset has 30 columns where all the column names start with 'inq'. So, with starts_with, it returns all columns, and adding ends_with, it is checking an OR match, e.g.

    sf_df %>% 
         select(starts_with("inq"), ends_with("5")) %>%
         ncol
    #[1] 30 # returns 30 columns
    

    It is not removing the columns that have no match for 5 at the string

    It is not a behavior of the order of arguments as

    sf_df %>%
      select(ends_with("5"), starts_with("inq")) %>%
      ncol
    #[1] 30
    

    Now, if we use only ends_with

    sf_df %>% 
      select(ends_with("5")) %>% 
      ncol
    #[1] 4
    

    Based on the example, all columns starts with 'inq', so, ends_with alone would be sufficient for a single string match as the documentation for ?ends_with specifies

    match - A string.

    and not multiple strings

    where the Usage is

    starts_with(match, ignore.case = TRUE, vars = peek_vars())