A logical expression that would evaluate the n-th column say the 10th can be used in "i" argument to "["
.
n=10
shorterM <- M[ M[,n] >= "20080101" & M[,n] <= "20100101" , ]
This should work for either a matrix or a dataframe as long as these "dates" are actually character values with that format. The ">=","<=" abd "&" operators are all vectorized. This is "logical indexing". You do take a risk in posting questions with no code since most respondents think that is your job and may not test (as I have not). Next time post a small example, preferably with the dput
function, and specify what the right answer is. Then you get tested code and everyone is happy and you get no close votes ... unless of course this is a duplicate which is certainly possible.
The offered example is used as a worked example:
> DD <- read.table(text="2008010106 a
+ 2008010112 b
+ 2008010118 f
+ 2008010206 e
+ 2008010200 w
+ 2008010212 a
+ 2008010218 b
+ 2008010300 f
+ 2008010406 e
+ 2008010306 a
+ 2008010312 b
+ 2008010318 f
+ 2008010400 r
+ 2008010412 e", colClasses="character")
> (shorterDD <- DD[ DD[,1] >= "2008010200" & DD[,1] <= "2008010412" , ])
V1 V2
4 2008010206 e
5 2008010200 w
6 2008010212 a
7 2008010218 b
8 2008010300 f
9 2008010406 e
10 2008010306 a
11 2008010312 b
12 2008010318 f
13 2008010400 r
14 2008010412 e