按月子集数据

时间:2019-07-30 05:41:03

标签: r dplyr grouping subset

按月划分的子集数据仅包括3月,6月,9月,12月。

设置:


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")

我希望最终矩阵仅包含名称包含“ Mar”,“ Jun”,“ Sep”,“ Dec”的列。输出应采用以下形式:

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

1 个答案:

答案 0 :(得分:5)

我们可以使用grep选择带有模式的列

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

或者在dplyr中,我们可以使用select

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

数据

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")