Lets say I have a data frame that looks like this
ca01<- c(1:10)
ca02<- c(2:11)
ca03<- c(3:12)
stuff.1<- rep('test',10)
other<- rep(9,10)
data<- data.frame(ca01,ca02,ca03,stuff.1,other)
I then create a vector that contains the column names
samps<- colnames(data)
I then want to filter this vector to only contain items that begin with the prefix "ca". I do not want to exclude stuff.1 and other by writing individual lines of code that remove these specifically, so something like
samps<-samps[samps!='stuff.1']
samps<-samps[samps!='other']
would not be suitable.
Try using grepl
> Names <- colnames(data)
> Names[grepl("^ca", Names)]
[1] "ca01" "ca02" "ca03"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With