[R] dplyr - add/expand rows
William Dunlap
wdunlap at tibco.com
Sun Nov 26 01:49:36 CET 2017
dplyr may have something for this, but in base R I think the following does
what you want. I've shortened the name of your data set to 'd'.
i <- rep(seq_len(nrow(d)), d$YEAR_TO-d$YEAR_FROM+1)
j <- sequence(d$YEAR_TO-d$YEAR_FROM+1)
transform(d[i,], YEAR=YEAR_FROM+j-1, YEAR_FROM=NULL, YEAR_TO=NULL)
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Sat, Nov 25, 2017 at 11:18 AM, Hutchinson, David (EC) <
david.hutchinson at canada.ca> wrote:
> I have a returned tibble of station operational record similar to the
> following:
>
> > data.collection
> # A tibble: 5 x 4
> STATION_NUMBER YEAR_FROM YEAR_TO RECORD
> <chr> <int> <int> <chr>
> 1 07EA001 1960 1960 QMS
> 2 07EA001 1961 1970 QMC
> 3 07EA001 1971 1971 QMM
> 4 07EA001 1972 1976 QMC
> 5 07EA001 1977 1983 QRC
>
> I would like to reshape this to one operational record (row) per year per
> station. Something like:
>
> 07EA001 1960 QMS
> 07EA001 1961 QMC
> 07EA001 1962 QMC
> 07EA001 1963 QMC
> ...
> 07EA001 1971 QMM
>
> Can this be done in dplyr easily?
>
> Thanks in advance,
>
> David
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list