Grep a 1 or 2 character number from a variable in R -
i have 2 tables, grow
, temp
. grow
details growing , nongrowing seasons each area i'm concerned (these static), while temp
details average temperature each month of wide range of years (these columns) same areas. i'm trying identify average temperature each year growing , nongrowing seasons, i've run problem how pick columns - i'm using grep
, can't figure out how account two-digit months!
at point i've isolated each column of years, , have vector of column names single year - "tmp_2001_1" "tmp_2001_2" "tmp_2001_3" ... "tmp_2001_11" "tmp_2001_12"
. have, stored in pair of variables, start , end of growing season, stored integers representing months: start <- number between 1 , 12
, end <- number between 1 , 12
. can't figure out how grep
identify growing season when start
or end
has 2 digits. have works case of them both being 1 digit numbers:
grow_months <- grep(paste('tmp_','2001','_','[',start,'-',end,']',sep = ''), vector_of_column_names)
how can expand account cases start
or end
double digit?
as gregor suggests,
strings = paste0("tmp_2001_",c(1:20,100)) start = 9 end = 12 ii = sapply(strsplit(strings,'_'), function(x)x[3]%in%start:end) strings[ii]
or using easy regex try
ii = grep(paste(paste0('_(',start:end,')$'),collapse='|'), strings) strings[ii]
Comments
Post a Comment