Dear R users, Please help me to detect consecutive n values in R and their interval. rle.seq1<-rle(reco$extr) cbind(rle.seq1$values) index<-any(rle.seq1$values=="DRY"&rle.seq1$lengths>=3) cumsum(rle.seq1$lengths)[index] reco is a data frame with 2 columns (year and values (DRY, WET). I want to have something like this: 1799-1800 - WET - 2 1803-1805 - WET - 3 Thank you very much! All the best! Catalin year extr 1 1768 <NA> 2 1769 <NA> 3 1770 <NA> 4 1771 <NA> 5 1772 <NA> 6 1773 DRY 7 1774 <NA> 8 1775 <NA> 9 1776 <NA> 10 1777 DRY 11 1778 <NA> 12 1779 DRY 13 1780 DRY 14 1781 <NA> 15 1782 DRY 16 1783 <NA> 17 1784 <NA> 18 1785 <NA> 19 1786 <NA> 20 1787 <NA> 21 1788 <NA> 22 1789 <NA> 23 1790 <NA> 24 1791 <NA> 25 1792 <NA> 26 1793 <NA> 27 1794 <NA> 28 1795 WET 29 1796 <NA> 30 1797 <NA> 31 1798 <NA> 32 1799 WET 33 1800 WET 34 1801 <NA> 35 1802 <NA> 36 1803 WET 37 1804 WET 38 1805 WET 39 1806 <NA> 40 1807 <NA> 41 1808 <NA> 42 1809 <NA> 43 1810 WET 44 1811 <NA> 45 1812 <NA> 46 1813 WET 47 1814 DRY 48 1815 <NA> 49 1816 <NA> 50 1817 <NA> 51 1818 <NA> 52 1819 <NA> 53 1820 <NA> 54 1821 <NA> 55 1822 WET 56 1823 WET 57 1824 WET 58 1825 WET 59 1826 <NA> 60 1827 DRY 61 1828 DRY 62 1829 WET 63 1830 WET 64 1831 WET 65 1832 WET 66 1833 <NA> 67 1834 DRY 68 1835 DRY 69 1836 <NA> 70 1837 <NA> 71 1838 <NA> 72 1839 <NA> 73 1840 <NA> 74 1841 <NA> 75 1842 <NA> 76 1843 WET 77 1844 WET 78 1845 <NA> 79 1846 <NA> 80 1847 <NA> 81 1848 DRY 82 1849 DRY 83 1850 <NA> 84 1851 <NA> 85 1852 WET 86 1853 <NA> 87 1854 <NA> 88 1855 <NA> 89 1856 <NA> 90 1857 WET 91 1858 <NA> 92 1859 <NA> 93 1860 <NA> 94 1861 <NA> 95 1862 <NA> 96 1863 <NA> 97 1864 <NA> 98 1865 <NA> 99 1866 DRY 100 1867 DRY 101 1868 <NA> 102 1869 DRY 103 1870 <NA> 104 1871 WET 105 1872 <NA> 106 1873 <NA> 107 1874 DRY 108 1875 DRY 109 1876 <NA> 110 1877 <NA> 111 1878 <NA> 112 1879 <NA> 113 1880 WET 114 1881 WET 115 1882 <NA> 116 1883 <NA> 117 1884 <NA> 118 1885 <NA> 119 1886 DRY 120 1887 DRY 121 1888 DRY 122 1889 <NA> 123 1890 <NA> 124 1891 <NA> 125 1892 <NA> 126 1893 <NA> 127 1894 <NA> 128 1895 <NA> 129 1896 <NA> 130 1897 <NA> 131 1898 <NA> 132 1899 <NA> 133 1900 <NA> 134 1901 <NA> 135 1902 <NA> 136 1903 WET 137 1904 <NA> 138 1905 <NA> 139 1906 WET 140 1907 <NA> 141 1908 <NA> 142 1909 <NA> 143 1910 <NA> 144 1911 WET 145 1912 WET 146 1913 WET 147 1914 WET 148 1915 <NA> 149 1916 <NA> 150 1917 <NA> 151 1918 DRY 152 1919 <NA> 153 1920 <NA> 154 1921 DRY 155 1922 <NA> 156 1923 <NA> 157 1924 <NA> 158 1925 <NA> 159 1926 <NA> 160 1927 <NA> 161 1928 DRY 162 1929 <NA> 163 1930 <NA> 164 1931 <NA> 165 1932 <NA> 166 1933 <NA> 167 1934 <NA> 168 1935 <NA> 169 1936 <NA> 170 1937 <NA> 171 1938 <NA> 172 1939 <NA> 173 1940 <NA> 174 1941 <NA> 175 1942 <NA> 176 1943 <NA> 177 1944 WET 178 1945 <NA> 179 1946 DRY 180 1947 DRY 181 1948 DRY 182 1949 <NA> 183 1950 WET 184 1951 <NA> 185 1952 DRY 186 1953 <NA> 187 1954 <NA> 188 1955 WET 189 1956 <NA> 190 1957 <NA> 191 1958 <NA> 192 1959 WET 193 1960 <NA> 194 1961 <NA> 195 1962 <NA> 196 1963 <NA> 197 1964 DRY 198 1965 <NA> 199 1966 <NA> 200 1967 <NA> 201 1968 DRY 202 1969 WET 203 1970 WET 204 1971 <NA> 205 1972 <NA> 206 1973 <NA> 207 1974 WET 208 1975 WET 209 1976 <NA> 210 1977 <NA> 211 1978 <NA> 212 1979 <NA> 213 1980 <NA> 214 1981 <NA> 215 1982 WET 216 1983 DRY 217 1984 <NA> 218 1985 DRY 219 1986 <NA> 220 1987 <NA> 221 1988 WET 222 1989 WET 223 1990 WET 224 1991 <NA> 225 1992 <NA> 226 1993 <NA> 227 1994 <NA> 228 1995 DRY 229 1996 DRY 230 1997 WET 231 1998 <NA> 232 1999 <NA> 233 2000 DRY 234 2001 <NA> 235 2002 <NA> 236 2003 DRY 237 2004 <NA> 238 2005 <NA> 239 2006 <NA> 240 2007 <NA> -- - - Catalin-Constantin ROIBU Lecturer PhD, Forestry engineer Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 FAX: +4 0230 52 16 64 silvic.usv.ro <http://www.usv.ro/> [image: Mailtrack] <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&> Sender notified by Mailtrack <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&> 04.06.20, 16:04:35 [[alternative HTML version deleted]]
Catalin,> On Jun 4, 2020, at 6:06 AM, Catalin Roibu <catalinroibu at gmail.com> wrote: > > Dear R users, > > Please help me to detect consecutive n values in R and their interval. > > > rle.seq1<-rle(reco$extr) > cbind(rle.seq1$values) > index<-any(rle.seq1$values=="DRY"&rle.seq1$lengths>=3) > cumsum(rle.seq1$lengths)[index] > > reco is a data frame with 2 columns (year and values (DRY, WET). > > I want to have something like this: > 1799-1800 - WET - 2 > 1803-1805 - WET - 3 > > Thank you very much!Something like: wd.rle <- rle(reco$extr) is.wet <- wd.rle[["values"]]=="WET" wd.rle[["values"]] <- ifelse(is.wet, cumsum( is.wet ), 0) wet.list <- split( reco$year, inverse.rle( wd.rle ) )[ -1 ] sapply( wet.list[ lengths(wet.list) > 1 ], range) should get you started. The last line returns: : 2 3 6 7 8 12 15 20 21 23 : [1,] 1799 1803 1822 1829 1843 1880 1911 1969 1974 1988 : [2,] 1800 1805 1825 1832 1844 1881 1914 1970 1975 1990 You can use `apply' to further process this to get the desired format for your result. I assume here that reco$years are in groups of consecutive 'WET' years. If there are gaps or other oddities you will need to replace `range' with a function that handles that. HTH, Chuck