简体   繁体   中英

How to find increasing or decreasing trend in a matrix in r?

I have a time series matrix called trendtable , which has data's from 1960 to 2010(57 columns) for 175 countries .The column names are years and their corresponding values are given.I need to find the difference between the columns to find the trend for each country, to find if the trend is going up or down . The resulting trend value should be in new table.The code I have written is mentioned below,but i guess its wrong.

for (i in 1:175) {
  Trend=0

for (j in 5:56) {


   Dif=TimeSeriesCO2[i,j]-TimeSeriesCO2[i,j+1]
   if(Dif<0){ 
     Trend--}
   else{
     Trend++}
   }
  TrendTable<-rbind(TrendTable,Trend)
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM