0
$\begingroup$

I'm doing some cluster analysis on the MLTobs from the LifeTables package and have come across a tricky problem plotting frequency of the Year variable in the mlt.mx.info dataframe. Year contains the period that the life table was measured over, in intervals. Here's a table of the data:

    1751-1754 1755-1759 1760-1764 1765-1769 1770-1774 1775-1779 1780-1784 1785-1789 1790-1794 
        1         1         1         1         1         1         1         1         1 
1795-1799 1800-1804 1805-1809 1810-1814 1815-1819 1816-1819 1820-1824 1825-1829 1830-1834 
        1         1         1         1         1         2         3         3         3 
1835-1839 1838-1839 1840-1844 1841-1844 1845-1849 1846-1849 1850-1854 1855-1859 1860-1864 
        4         1         5         3         8         1        10        11        11 
1865-1869 1870-1874 1872-1874 1875-1879 1876-1879 1878-1879 1880-1884 1885-1889 1890-1894 
       11        11         1        12         2         1        15        15        15 
1895-1899 1900-1904 1905-1909 1908-1909 1910-1914 1915-1919 1920-1924 1921-1924 1922-1924 
       15        15        15         1        16        16        16         2         1 
1925-1929 1930-1934 1933-1934 1935-1939 1937-1939 1940-1944 1945-1949 1947-1949 1948-1949 
       19        19         1        20         1        22        22         3         1 
1950-1954 1955-1959 1956-1959 1958-1959 1960-1964 1965-1969 1970-1974 1975-1979 1980-1984 
       30        30         2         1        40        40        41        41        41 
1983-1984 1985-1989 1990-1994 1991-1994 1992-1994 1995-1999 2000-2003 2000-2004 2005-2006 
        1        42        42         1         1        44         3        41        22 
2005-2007 
       14 

As you can see, some of the intervals sit within other intervals. Thankfully none of them overlap. I want to simplify the intervals so intervals such as 1992-1994 and 1991-1994 all go into 1990-1994.

An idea might be to get the modulo of each interval and sort them into their new intervals that way but I'm unsure how to do this with the interval data type. If anyone has any ideas I'd really appreciate the help. Ultimately I want to create a histogram or barplot to illustrate the nicely.

$\endgroup$
1
$\begingroup$

This was a x-post. There is a great solution over here, where I originally asked the question.

If I understand your problem, you'll want something like this:

bottom <- seq(1750, 2010, 5)
library(dplyr)
new_df <- mlt.mx.info %>%
  arrange(Year) %>%
  mutate(year2 = as.numeric(substr(Year, 6, 9))) %>%
  mutate(new_year = paste0(bottom[findInterval(year2, bottom)], "-",(bottom[findInterval(year2, bottom) + 1] - 1)))
View(new_df)

So what this does, it creates bins, and outputs a new column (new_year) that is the bottom of the bin. So everything from 1750-1754 will correspond to a new value of 1750-1754 (in string form; the original is an integer type, not sure how to fix that). Does this do what you want? Double check the results, but it looks right to me. @goodtimeslim

$\endgroup$
-2
$\begingroup$

Use library(reshape) for most of the pivot & filter operations. Should resolve your problem.

$\endgroup$
2
  • $\begingroup$ Could you expand upon this answer or provide an example of its application? $\endgroup$ – Ryan J. Smith May 10 '15 at 17:48
  • $\begingroup$ # example of melt function library(reshape) mdata <- melt(mydata, id=c("id","time")) $\endgroup$ – Dheeraj Joshi May 19 '16 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.