r - return column index condition on column names -
i have data frame named dat
. has columns prefix vix
.
eg.
date, vo, vix2, vix3, vix4, ... vix24, other columns
in case,
start = which(names(dat) == "vix2")
end = which(names(dat) == "vix24")
but problem is, don't know number arrangement after vix
. in case, there way return start
, end
?
you can try
range(grep('vix', names(dat)))
suppose if columns not consecutive
v1 <- c('date', 'vix2', 'v0', 'vix3', 'v5', 'vix4') range(grep('vix', v1)) #[1] 2 6
will give first , last entry 'vix', if going subset dataset 'vix' columns, don't need range
, grep
enough
grep('vix', v1) #[1] 2 4 6
Comments
Post a Comment