根据最后一位数字对M3 MCOMP进行子集

时间:2019-12-19 16:02:38

标签: r

当查看每月的时间序列时 ID在[1501、2800]之间的M3比赛。如何对所有以2结尾的ID进行子集化?

我已经尝试了各种for和if循环,但是我想知道是否有一种方法可以使用子集函数来做到这一点?

谢谢

1 个答案:

答案 0 :(得分:0)

一种选择是在将ID设置为1500之间之后,在数据集的grep上使用names,其模式与字符串的末尾($)匹配'2'和2800

library(Mcomp)
lst1 <- lapply(M3, I)
v1 <- sub("^N", "", names(lst1))
i1 <- v1 > 1500 & v1 <= 2800
lst2 <- lst1[i1][grep("2$", names(lst1[i1]))] 
length(lst2)
#[1] 130

names(lst2)
#[1] "N1502" "N1512" "N1522" "N1532" "N1542" "N1552" "N1562" "N1572" "N1582" "N1592" "N1602" "N1612" "N1622" "N1632" "N1642" "N1652" "N1662"
# [18] "N1672" "N1682" "N1692" "N1702" "N1712" "N1722" "N1732" "N1742" "N1752" "N1762" "N1772" "N1782" "N1792" "N1802" "N1812" "N1822" "N1832"
# [35] "N1842" "N1852" "N1862" "N1872" "N1882" "N1892" "N1902" "N1912" "N1922" "N1932" "N1942" "N1952" "N1962" "N1972" "N1982" "N1992" "N2002"
# [52] "N2012" "N2022" "N2032" "N2042" "N2052" "N2062" "N2072" "N2082" "N2092" "N2102" "N2112" "N2122" "N2132" "N2142" "N2152" "N2162" "N2172"
# [69] "N2182" "N2192" "N2202" "N2212" "N2222" "N2232" "N2242" "N2252" "N2262" "N2272" "N2282" "N2292" "N2302" "N2312" "N2322" "N2332" "N2342"
# [86] "N2352" "N2362" "N2372" "N2382" "N2392" "N2402" "N2412" "N2422" "N2432" "N2442" "N2452" "N2462" "N2472" "N2482" "N2492" "N2502" "N2512"
#[103] "N2522" "N2532" "N2542" "N2552" "N2562" "N2572" "N2582" "N2592" "N2602" "N2612" "N2622" "N2632" "N2642" "N2652" "N2662" "N2672" "N2682"
#[120] "N2692" "N2702" "N2712" "N2722" "N2732" "N2742" "N2752" "N2762" "N2772" "N2782" "N2792"