Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select columns that do NOT start with a string using dplyr in R

I want to select columns from my tibble that end with the letter R AND do NOT start with a character string ("hc"). For instance, if I have a dataframe that looks like this:

name  hc_1  hc_2  hc_3r  hc_4r  lw_1r  lw_2  lw_3r  lw_4   
Joe   1     2     3      2      1      5     2      2
Barb  5     4     3      3      2      3     3      1

To do what I want, I've tried many options, but I'm surprised that this one doesn't work:

library(tidyverse)
data %>%
  select(ends_with("r"), !starts_with("hc"))

When I try it, I get this error:

Error: !starts_with("hc") must evaluate to column positions or names, not a logical vector

I've also tried using negate() and get the same error.

library(tidyverse)
data %>%
  select(ends_with("r"), negate(starts_with("hc")))

Error: negate(starts_with("hc")) must evaluate to column positions or names, not a function

I'd like to keep the answer within the dplyr select function because, once I select the variables, I'm going to end up reversing them by using mutate_at, so a tidy solution is best.

Thank you!

like image 925
J.Sabree Avatar asked Aug 30 '19 17:08

J.Sabree


2 Answers

We can use - as the starts_with output is not a logical vector

library(dplyr)
data %>%
     select(ends_with("r"), -starts_with("hc"))
 #   lw_1r lw_3r
 #1     1     2
 #2     2     3

data

data <- structure(list(name = c("Joe", "Barb"), hc_1 = c(1L, 5L), hc_2 = c(2L, 
4L), hc_3r = c(3L, 3L), hc_4r = 2:3, lw_1r = 1:2, lw_2 = c(5L, 
3L), lw_3r = 2:3, lw_4 = 2:1), class = "data.frame", row.names = c(NA, 
-2L))
like image 60
akrun Avatar answered Nov 09 '22 06:11

akrun


If you need an advanced regular expression use matches

library(dplyr)
#Starts with any letter except h or c and ends with an r
df %>% select(matches('^[^hc].*r$'))
  lw_1r lw_3r
1     1     2
2     2     3
like image 38
A. Suliman Avatar answered Nov 09 '22 05:11

A. Suliman