Here’s a few examples of how you might use these techniques in with some toy data: You can also use helpers contains() and matches() for more flexibly matching.īy type: df %>% select(where(is.numeric)), df %>% select(where(is.factor)).īy any combination of the above using the Boolean operators !, &, and |:ĭf %>% select(!where(is.factor)): selects all non-factor variables.ĭf %>% select(where(is.numeric) & starts_with("x")): selects all numeric variables that starts with “x”.ĭf %>% select(starts_with("a") | ends_with("z")): selects all variables that starts with “a” or ends with “z”.ĭetailed analysis showing the challenges of the previous approach, and motivating us to do better.) Selecting by position is not generally recommended, but rename()ing by position can be very useful, particularly if the variable names are very long, non-syntactic, or duplicated.īy name: df %>% select(a, e, j), df %>% select(c(a, e, j)) or df %>% select(a:d).īy function of name: df %>% select(starts_with("x")), or df %>% select(ends_with("s")). There are now five ways to select variables in select() and rename():īy position: df %>% select(1, 5, 10) or df %>% select(1:4). Select() and rename() are now significantly more flexible thanks to enhancements to the The rest of this post has been updated accordingly. # Attempts to invoke `data()` function ame ( x = 1 ) %>% select ( data )
0 Comments
Leave a Reply. |