DD chen
DD chen

Reputation: 189

data.frame Select columns with condition

I have 2 questions but they are very similar:

1) I have a dataframe: test<-data.frame(NAT1=1,NAT2=2,NAT3=3,NAT_S1=4,NAT_S2=5):

  NAT1 NAT2 NAT3 NAT_S1 NAT_S2
1    1    2    3      4      5

I want to select column which names start with NAT and end with a number:

  NAT1 NAT2 NAT3 
1    1    2    3   

2) Same thing I want to do with characters: colnames(test) I want to keep only : [1] "NAT1" "NAT2" "NAT3"

I suppose there are a function could do this. Thanks for your help

Upvotes: 0

Views: 40

Answers (2)

akrun
akrun

Reputation: 887831

With dplyr, we can use matches

library(dplyr)
test %>%
   select(matches("^NAT.*\\d+$"))

Upvotes: 0

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21442

test[, grepl("^NAT\\d$", names(test))]
  NAT1 NAT2 NAT3
1    1    2    3

If you have more than 1 number after NAT, you can use the quantifier + or, if you want to select a certain number of numbers or a certain range of numbers, quantification with { }:

test[, grepl("^NAT\\d+$", names(test))] # matches one or more numbers
test[, grepl("^NAT\\d{2}$", names(test))] # matches exactly 2 numbers
test[, grepl("^NAT\\d{2,4}$", names(test))] # matches the range of 2 to 4 numbers
test[, grepl("^NAT\\d{2,}$", names(test))] # matches the range of 2 to any number of numbers

Alternatively, you can use str_detectfrom the package stringr:

test[, str_detect(colnames(test), "^NAT\\d$")]
  NAT1 NAT2 NAT3
1    1    2    3

Upvotes: 2

Related Questions