Reputation: 1537
I have a data frame named dat
. It has some columns with prefix vix
.
Eg.
Date, VO, vix2, vix3, vix4, ... vix24, other columns
In this case,
start = which(names(dat) == "vix2")
end = which(names(dat) == "vix24")
But the problem is, I don't know the number arrangement after vix
. In this case, is there a way to return start
and end
?
Upvotes: 2
Views: 128
Reputation: 887891
You can try
range(grep('vix', names(dat)))
Suppose if the columns are not consecutive
v1 <- c('Date', 'vix2', 'v0', 'vix3', 'V5', 'vix4')
range(grep('vix', v1))
#[1] 2 6
will give the first and last entry with 'vix', but if you are going to subset the dataset for 'vix' columns, you don't need the range
, just grep
will be enough
grep('vix', v1)
#[1] 2 4 6
Upvotes: 2