JC3019
JC3019

Reputation: 383

Subset data by Month

Subset data by Month to only include Mar, Jun, Sep, Dec.

setup:


x1 <- rnorm(24,0,1)
x2 <- rnorm(24,0,1)
x3 <- rnorm(24,0,1)

mat1 <- data.frame(rbind(x1,x2,x3))
colnames(mat1) <- c("Jan.96", "Feb.96", "Mar.96", "Apr.96", "May.96", "Jun.96", "Jul.96", "Aug.96", "Sep.96", "Oct.96", "Nov.96", "Dec.96", "Jan.97", "Feb.97", "Mar.97", "Apr.97", "May.97", "Jun.97", "Jul.97", "Aug.97", "Sep.97", "Oct.97", "Nov.97", "Dec.97")

I want the final matrix to only include columns whose name contains "Mar", "Jun", "Sep", "Dec". Output should be of the form:

output <- cbind(mat1$Mar.96, mat1$Jun.96, mat1$Sep.96, mat1$Dec.96, mat1$Mar.97, mat1$Jun.97, mat1$Sep.97, mat1$Dec.97)

  [,1]       [,2]       [,3]       [,4]       [,5]       [,6]      [,7]       [,8]
[1,]  0.5179178 -0.4810577  0.2178482 -0.4867642 -0.1219542  0.3185248  1.464423  0.4775712
[2,]  0.4905709  1.2061020 -0.6434293 -0.1864487 -0.2297027 -0.3290413 -3.438259 -0.8218304
[3,] -0.1467610 -0.8399577 -0.4046136  0.1745972  0.7781311  0.9401594 -1.073207  1.2710265

Upvotes: 0

Views: 59

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

We can use grep to select columns with a pattern

mat1[, grep("Mar|Jun|Sep|Dec", names(mat1))]

#    Mar.96 Jun.96   Sep.96  Dec.96   Mar.97   Jun.97   Sep.97   Dec.97
#x1 1.55871 1.7151 -0.68685 0.35981 -0.55584 -1.96662 -1.06782 -0.72889
#x2 0.83779 1.2538  0.89513 0.68864 -0.30596 -0.20792  1.20796 -0.46666
#x3 0.25332 1.3686 -1.54875 0.21594 -0.33321  0.30353  0.92227 -2.30917

Or in dplyr, we can use select

library(dplyr)
mat1 %>% select(matches("Mar|Jun|Sep|Dec"))

data

set.seed(123)
x1 <- rnorm(24,0,1)
x2 <- rnorm(24,0,1)
x3 <- rnorm(24,0,1)

mat1 <- data.frame(rbind(x1,x2,x3))
colnames(mat1) <- c("Jan.96", "Feb.96", "Mar.96", "Apr.96", "May.96", 
"Jun.96", "Jul.96", "Aug.96", "Sep.96", "Oct.96", "Nov.96", "Dec.96", 
"Jan.97", "Feb.97", "Mar.97", "Apr.97", "May.97", "Jun.97", "Jul.97", 
"Aug.97", "Sep.97", "Oct.97", "Nov.97", "Dec.97")

Upvotes: 4

Related Questions