0

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)
}
  • Have you checked `trend` package https://cran.r-project.org/web/packages/trend/trend.pdf? – shiny Mar 22 '17 at 03:30
  • You might need to check `wq` package https://www.rdocumentation.org/packages/wq/versions/0.4.8 – shiny Mar 22 '17 at 03:36
  • I referred this code form some other discussion ,but the problem seems to be because of the syntax error. I am new to R – jeffy abraham Mar 22 '17 at 03:40
  • You might get more response if you made this question reproducible. There are even questions on StackOverflow for how to present [reproducible questions](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), as well as within the [StackOverflow Help](http://stackoverflow.com/help/mcve) itself. – r2evans Mar 22 '17 at 05:00

1 Answers1

0

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 transpose the result from apply is perhaps unintuitive but a fact of dealing with it. There may be ways to do it without the transpose, 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.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • You need to provide some expected output. I don't understand this additional point to your question, and it was not very clear before that. – r2evans Mar 22 '17 at 05:36