[R] Capturing positive and negative changes using R
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Jul 20 22:16:18 CEST 2019
Hello,
Please don't post in HTML, the data is unreadable.
Two ideas:
1) Why c(FALSE, diff()) if diff returns numeric values? Use c(0, diff)
instead, and you won't need the coercion to numeric with the plus sign.
2) There are many ways to group by a variable, in this case 'Country'.
See in base R
?aggregate
?tapply
?ave
?by
And there's also contributed packages, such as dplyr and data.table.
If you can repost your data, setting your e-mail client to plain text,
we will be able to say more.
Hope this helps,
Rui Barradas
Às 20:33 de 20/07/19, Faradj Koliev escreveu:
> Dear R-users,
>
> I have a country-year data for 180 countries from 1970 to 2010. I’m interested in capturing positive and negative changes in some of the variables. Some of these variables are continuous (0,25, 0,33, 1, 1,5 etc) others are ordered (0,1, 2).
>
> To do this, I use this code data$X1_change<- +c(FALSE,diff(data$X1))
>
> My data looks something like this (please see below).
>
> There’re some problems with this code: (1) I can’t capture the smaller changes, say from 0,25 to 0,33 ( I get weird numbers). I would love to get the exact difference ( for ex: +1, -0,22, +4, -2 etc). (2) It can’t make difference between countries. That is, it takes the difference between countries while it should only do this for each country ( for ex: when the US ends in 2011, and Canada starts, it counts this a difference but it shouldn’t, see below). (3) NAs, missing values, is neither a positive or negative change, although it does think that what comes after the NA is a difference.
>
> So, I wonder if anyone here can help me to adjust this code. I appreciate all comments.
>
>
> Year
> Country
> X1
> X2
> 1990
> United States
> 0
> 0,22
> 1991
> United States
> 0
> 0,22
> 1992
> United States
> 0
> 0,22
> 1993
> United States
> 0
> 0,22
> 1994
> United States
> 0
> 0,22
> 1995
> United States
> 0
> 0,22
> 1996
> United States
> 0
> 0,22
> 1997
> United States
> 0
> 0,5
> 1998
> United States
> 0
> 0,5
> 1999
> United States
> 0
> 0,5
> 2000
> United States
> 0
> 0,5
> 2001
> United States
> 0
> 0,5
> 2002
> United States
> 2
> NA
> 2003
> United States
> 2
> 0,5
> 2004
> United States
> 2
> 1
> 2005
> United States
> 1
> 1
> 2006
> United States
> 1
> 1
> 2007
> United States
> 1
> 1
> 2008
> United States
> 1
> 1
> 2009
> United States
> 1
> 1
> 2010
> United States
> 1
> 0,5
> 2011
> United States
> 0
> 0,5
> 1990
> Canada
> 1
> 1,5
> 1991
> Canada
> 1
> 1,5
> 1992
> Canada
> 1
> NA
> 1993
> Canada
> 1
> 1,5
> 1994
> Canada
> 1
> 1,5
> 1995
> Canada
> 1
> 1,5
> 1996
> Canada
> 1
> 1,5
> 1997
> Canada
> 1
> 1,5
> 1998
> Canada
> 1
> 2
> 1999
> Canada
> 2
> 2
> 2000
> Canada
> 2
> 2
> 2001
> Canada
> 2
> 2
> 2002
> Canada
> 2
> 2
> 2003
> Canada
> 1
> 2
> 2004
> Canada
> 2
> 0,5
> 2005
> Canada
> 1
> 0,5
> 2006
> Canada
> 0
> 0,5
> 2007
> Canada
> 1
> 0,5
> 2008
> Canada
> 0
> 0,5
> 2009
> Canada
> 1
> 0,5
> 2010
> Canada
> 1
> 0,5
> 2011
> Canada
> 0
> 1
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list