Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the difference in value between subsequent observations (country-years)?
    primarykey
    data
    text
    <p>Let's say, I have scores for 5 countries over a period of 10 years such as:</p> <pre><code>mydata&lt;-1:3 mydata&lt;-expand.grid( country=c('A', 'B', 'C', 'D', 'E'), year=c('1980','1981','1982','1983','1984','1985','1986','1987','1988','1989')) mydata$score=sapply(runif(50,0,2), function(x) {round(x,4)}) library(reshape) mydata&lt;-reshape(mydata, v.names="score", idvar="year", timevar="country", direction="wide") &gt; head(mydata) year score.A score.B score.C score.D score.E 1 1980 1.0538 1.6921 1.3165 1.7434 1.9687 6 1981 1.4773 1.6479 0.3135 0.6172 0.7704 11 1982 0.8748 1.3704 0.2788 1.6306 1.7237 16 1983 1.1224 1.1340 1.7684 1.3352 0.4317 21 1984 1.5496 1.8706 1.4641 0.5313 0.8590 26 1985 1.7715 1.8953 0.6230 0.3580 1.6313 </code></pre> <p>Now, I would like to create a new variable "period" that is 1 if the score of the subsequent year is +/- 0.5 different from the score of the previous year and that is 0 if this is not true. I would like to do so for all 5 countries. And it would be great if it were possible to identify the country-years for which period = 1 and display this information in a table.</p> <pre><code>&gt; head(mydata) year score.A score.B score.C score.D score.E period.A period.B ... 1 1980 1.0538 1.6921 1.3165 1.7434 1.9687 NA NA 6 1981 1.4773 1.6479 0.3135 0.6172 0.7704 0 .... 11 1982 0.8748 1.3704 0.2788 1.6306 1.7237 1 16 1983 1.1224 1.1340 1.7684 1.3352 0.4317 0 21 1984 1.5496 1.8706 1.4641 0.5313 0.8590 0 26 1985 1.7715 1.8953 0.6230 0.3580 1.6313 0 </code></pre> <p>I very much hope that this is not too much to ask. I tried it with <code>dist</code> in the <code>library(proxy)</code> but I do not know how to restrict the function to pairs of observation rather than the full row. Thanks a million!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload