Here's a stab, with fake data:
set.seed(42)
nc <- 9 ; nr <- 5
mtx <- t(replicate(nr, cumsum(sample(-3:3, size = nc, replace = TRUE))))
dimnames(mtx) <- list(LETTERS[seq_len(nr)], 2000 + seq_len(nc))
mtx
# 2001 2002 2003 2004 2005 2006 2007 2008 2009
# A 3 6 5 7 8 8 10 7 8
# B 1 1 3 6 4 4 7 10 7
# C 0 0 3 0 3 6 3 3 2
# D 3 3 5 7 9 8 9 6 8
# E -3 -5 -2 -1 -2 -2 -5 -2 -2
So I'm simulating your 175 countries with five letters, and 57 years with 9.
A first cut could be to determine if a year is greater than or equal to the previous year.
t(apply(mtx, 1, diff) >= 0)
# 2002 2003 2004 2005 2006 2007 2008 2009
# A TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE
# B TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE
# C TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE
# D TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE
# E FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE
(The need to t
ranspose the result from apply
is perhaps unintuitive but a fact of dealing with it. There may be ways to do it without the t
ranspose, but none that I know of in base R that are as direct.)
Note that there are one fewer columns than in the original dataset. I you want to keep track in each year if there is a cumulative increase or not, one might do:
t(apply(mtx, 1, function(a) cumsum(diff(a) >= 0)))
# 2002 2003 2004 2005 2006 2007 2008 2009
# A 1 1 2 3 4 5 5 6
# B 1 2 3 3 4 5 6 6
# C 1 2 2 3 4 4 5 5
# D 1 2 3 4 4 5 5 6
# E 0 1 2 2 3 3 4 5
But I don't necessarily find that more informative.
From your code, it looks like you might be expecting a single number for each country (letter), in which case you can simplify this further to:
apply(mtx, 1, function(a) sum(diff(a) >= 0))
# A B C D E
# 6 6 5 6 5
As far as adding this somehow to your current data, I am neither certain what you want (from your example) nor do I recommend appending summary data to the original matrix. The sole exception is for immediate visualization, not storage.