user243550
user243550

Reputation: 123

Choosing rows from a matrix in a specific order

I have a matrix of order 2192*23, I want to choose the rows number "3rd", "4th", "10th", "11th", "17th", "18th", "24th", "25th" and so on

[3,4,10,11,17,18,24,25,31,32,38,39,45,46,...]

continue till the last row in the above order. how I will do this in R?

Upvotes: 2

Views: 58

Answers (4)

Maël
Maël

Reputation: 51894

Another way is to use sequence

n = 2192/7
s <- sequence(nvec = rep(2, n), from = (7*1:n)-4)

# [1]    3    4   10   11   17   18   24   25   31   32   38   39   45   46   52   53   59   60   66   67   73   74
# ...

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388797

Using vector recycling -

ind <- seq_len(nrow(mat))
#ind <- seq_len(2192)
ind[c(F, F, T, T, F, F, F)]
#[1]    3    4   10   11   17   18   24   25   31   32   38   39   45   46   52
#[16]   53   59   60   66   67   73   74   80   81   87   88   94   95  101  102
#...
#...

Upvotes: 2

ThomasIsCoding
ThomasIsCoding

Reputation: 101014

We can use outer + seq

> c(outer(0:1, seq(3, 2192, by = 7), `+`))
  [1]    3    4   10   11   17   18   24   25   31   32   38   39   45   46   52
 [16]   53   59   60   66   67   73   74   80   81   87   88   94   95  101  102
 [31]  108  109  115  116  122  123  129  130  136  137  143  144  150  151  157
 [46]  158  164  165  171  172  178  179  185  186  192  193  199  200  206  207
 [61]  213  214  220  221  227  228  234  235  241  242  248  249  255  256  262
 [76]  263  269  270  276  277  283  284  290  291  297  298  304  305  311  312
 [91]  318  319  325  326  332  333  339  340  346  347  353  354  360  361  367
[106]  368  374  375  381  382  388  389  395  396  402  403  409  410  416  417
[121]  423  424  430  431  437  438  444  445  451  452  458  459  465  466  472
[136]  473  479  480  486  487  493  494  500  501  507  508  514  515  521  522
[151]  528  529  535  536  542  543  549  550  556  557  563  564  570  571  577
[166]  578  584  585  591  592  598  599  605  606  612  613  619  620  626  627
[181]  633  634  640  641  647  648  654  655  661  662  668  669  675  676  682
[196]  683  689  690  696  697  703  704  710  711  717  718  724  725  731  732
[211]  738  739  745  746  752  753  759  760  766  767  773  774  780  781  787
[226]  788  794  795  801  802  808  809  815  816  822  823  829  830  836  837
[241]  843  844  850  851  857  858  864  865  871  872  878  879  885  886  892
[256]  893  899  900  906  907  913  914  920  921  927  928  934  935  941  942
[271]  948  949  955  956  962  963  969  970  976  977  983  984  990  991  997
[286]  998 1004 1005 1011 1012 1018 1019 1025 1026 1032 1033 1039 1040 1046 1047
[301] 1053 1054 1060 1061 1067 1068 1074 1075 1081 1082 1088 1089 1095 1096 1102
[316] 1103 1109 1110 1116 1117 1123 1124 1130 1131 1137 1138 1144 1145 1151 1152
[331] 1158 1159 1165 1166 1172 1173 1179 1180 1186 1187 1193 1194 1200 1201 1207
[346] 1208 1214 1215 1221 1222 1228 1229 1235 1236 1242 1243 1249 1250 1256 1257
[361] 1263 1264 1270 1271 1277 1278 1284 1285 1291 1292 1298 1299 1305 1306 1312
[376] 1313 1319 1320 1326 1327 1333 1334 1340 1341 1347 1348 1354 1355 1361 1362
[391] 1368 1369 1375 1376 1382 1383 1389 1390 1396 1397 1403 1404 1410 1411 1417
[406] 1418 1424 1425 1431 1432 1438 1439 1445 1446 1452 1453 1459 1460 1466 1467
[421] 1473 1474 1480 1481 1487 1488 1494 1495 1501 1502 1508 1509 1515 1516 1522
[436] 1523 1529 1530 1536 1537 1543 1544 1550 1551 1557 1558 1564 1565 1571 1572
[451] 1578 1579 1585 1586 1592 1593 1599 1600 1606 1607 1613 1614 1620 1621 1627
[466] 1628 1634 1635 1641 1642 1648 1649 1655 1656 1662 1663 1669 1670 1676 1677
[481] 1683 1684 1690 1691 1697 1698 1704 1705 1711 1712 1718 1719 1725 1726 1732
[496] 1733 1739 1740 1746 1747 1753 1754 1760 1761 1767 1768 1774 1775 1781 1782
[511] 1788 1789 1795 1796 1802 1803 1809 1810 1816 1817 1823 1824 1830 1831 1837
[526] 1838 1844 1845 1851 1852 1858 1859 1865 1866 1872 1873 1879 1880 1886 1887
[541] 1893 1894 1900 1901 1907 1908 1914 1915 1921 1922 1928 1929 1935 1936 1942
[556] 1943 1949 1950 1956 1957 1963 1964 1970 1971 1977 1978 1984 1985 1991 1992
[571] 1998 1999 2005 2006 2012 2013 2019 2020 2026 2027 2033 2034 2040 2041 2047
[586] 2048 2054 2055 2061 2062 2068 2069 2075 2076 2082 2083 2089 2090 2096 2097
[601] 2103 2104 2110 2111 2117 2118 2124 2125 2131 2132 2138 2139 2145 2146 2152
[616] 2153 2159 2160 2166 2167 2173 2174 2180 2181 2187 2188

Upvotes: 2

Yuriy Saraykin
Yuriy Saraykin

Reputation: 8880

a1 <- c(3, 4)
d <- 7
n <- 10

S1 <- a1[1] + d * (seq(n) - 1)
S2 <- a1[2] + d * (seq(n) - 1)
sort(c(S1, S2))
#>  [1]  3  4 10 11 17 18 24 25 31 32 38 39 45 46 52 53 59 60 66 67

library(magrittr)
sapply(a1, function(x) (x + d * (seq(n) - 1))) %>% 
  matrix(ncol = 1) %>% 
  sort
#>  [1]  3  4 10 11 17 18 24 25 31 32 38 39 45 46 52 53 59 60 66 67

Created on 2022-01-18 by the reprex package (v2.0.1)

Upvotes: 2

Related Questions