dataframe - Counting back to events in R -


how count years since happened in r? data looks this:

                 year  fatal  non-fatal french airline   1989    1       1  french airline   1990    1       0  french airline   1991    0       0 french airline   1992    0       1 french airline   1993    0       0 uk airline       1989    1       1 uk airline       1990    0       0 uk airline       1991    1       0 uk airline       1992    0       0 uk airline       1993    0       0 

grouped airline, want have column counts when last fatal, or non-fatal crash happened. output this:

                 year  fatal  non-fatal  since fatal  since non-fatal                french airline   1989    1       1           0              0 french airline   1990    1       0           0              1 french airline   1991    0       0           1              2 french airline   1992    0       1           2              0  french airline   1993    0       0           3              1 uk airline       1989    1       1           0              0  uk airline       1990    0       0           1              1 uk airline       1991    1       0           0              2 uk airline       1992    0       0           1              3 uk airline       1993    0       0           2              4 

in ideal world i'd have column @ end counts kind of crash, fatal or non-fatal. how do it?

using joins , requiring version 1.9.7+ of package:

library(data.table)  # data borrowed @majom's answer  d[, sf :=    d[as.logical(fatal)][d, on=.(airline, year), roll=true, year-x.year]]  d[, sn :=    d[as.logical(non_fatal)][d, on=.(airline, year), roll=true, year-x.year]]  d[, sa :=    d[as.logical(pmax(fatal, non_fatal))][d, on=.(airline, year), roll=true, year-x.year]] 

which gives

           airline year fatal non_fatal sf sn sa  1: french_airline 1989     1         1  0  0  0  2: french_airline 1990     1         0  0  1  0  3: french_airline 1991     0         0  1  2  1  4: french_airline 1992     0         1  2  0  0  5: french_airline 1993     0         0  3  1  1  6:     uk_airline 1989     1         1  0  0  0  7:     uk_airline 1990     0         0  1  1  1  8:     uk_airline 1991     1         0  0  2  0  9:     uk_airline 1992     0         0  1  3  1 10:     uk_airline 1993     0         0  2  4  2 

alternately, last one: d[, sa := pmin(sf, sn)] if you've computed those.

the coercion logical should hint cols ought stored logical already...


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -