Question

I am having issues labeling the breaks (or intervals) when using the cut function.

Here are some sample data (the actual dataset has over 22,000 entries ranging from 1899-2014 within a dataframe with a total of 12 vectors):

x <- c(1899,1900,2001,2012,1999,1943,1944,1950,1988,1981,1988,1997,2014)

I'd like to categorize these by decade (eg. 1890s includes 1890 through 1899). I am able to make the breaks using the following code:

decade_vector <- cut(x, breaks = c(1889,1899,1909,1919,1929,1939,1949,
                                   1959,1969,1979,1989,1999,2009,2019))

However, when I go to label these breaks using the following code:

decade_vector <- cut(x, breaks = c(1889,1899,1909,1919,1929,1939,1949,
                                   1959,1969,1979,1989,1999,2009,2019), 
                     labels = c("1890s","1900s","1910s","1920s","1930s",
                                "1940s","1950s","1960s","1970s","1980s",
                                "1990s","2000s","2010s"), 
                     ordered=TRUE))

I get:

Warning message:
In is.factor(x) : NAs introduced by coercion

I created a dataframe and wrote it to csv, and when I look at it I don't see any NAs, but the labels are not there.

Can someone let me know what I'm doing wrong? I'd like to get various stats by decade, but I can't seem to figure out what I am missing here.

Any help would be super appreciated!

Thanks!

Was it helpful?

Solution

As the others already stated in the comments, your "NAs introduced by coercion" is not reproducible. But let me just give you a hint on how to make the code more "scalable" and readable:

x <- c(1890, 1899,1900,2001,2012,1999,1943,1944,1950,1988,1981,1988,1997,2014)
brk <- seq(1890, 2020, by=10) # breaks
cut(x, breaks=brk, right=FALSE, labels=paste(brk[-length(brk)], "s", sep=""), ordered=TRUE)
## [1] 1890s 1890s 1900s 2000s 2010s 1990s 1940s 1940s 1950s 1980s 1980s 1980s 1990s 2010s
## Levels: 1890s < 1900s < 1910s < 1920s < 1930s < 1940s < 1950s < 1960s < 1970s < 1980s < 1990s < 2000s < 2010s
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top