Reputation: 1399
I have two tables, Grow
and Temp
. Grow
details the growing and nongrowing seasons for each area I'm concerned with (these are static), while Temp
details the average temperature each month of a wide range of years (these are columns) for those same areas. I'm trying to identify the average temperature each year for the growing and nongrowing seasons, but I've run into a problem with how I pick the columns - I'm using grep
, but can't figure out how to account for two-digit months!
At this point I've isolated each column of years, and have a vector of column names for a single year - something like "tmp_2001_1" "tmp_2001_2" "tmp_2001_3" ... "tmp_2001_11" "tmp_2001_12"
. I also have, stored in a pair of variables, the start and end of growing season, which are stored as integers representing months: start <- some number between 1 and 12
, end <- some number between 1 and 12
. But I can't figure out how to get grep
to identify the growing season when start
or end
has 2 digits. This is what I have that works for the case of them both being 1 digit numbers:
grow_months <- grep(paste('tmp_','2001','_','[',start,'-',end,']',sep = ''), vector_of_column_names)
How can I expand this to account for cases where start
or end
is double digit?
Upvotes: 0
Views: 134
Reputation: 454
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 an easy regex you could try
ii = grep(paste(paste0('_(',start:end,')$'),collapse='|'), strings)
strings[ii]
Upvotes: 1