[R] dplyr - add/expand rows
David Winsemius
dwinsemius at comcast.net
Sun Nov 26 18:52:41 CET 2017
> On 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?
Probably, yes. This looks like a feasible plan might be to "fill-in" the gaps with a last observation carried forward value within categories of station number. The na.locf function in package zoo is very handy for some of these tasks. Or.... Perhaps merging this data with a skeleton data object with station numbers and a `seq`-built vectors for the range of years. Why don't you post a data example with sufficient complexity to represent the problem? Perhaps:
dput( head( data.collection, 20) )
It's clear that the first 5 lines are not sufficient since there's only one station. It's kind of a pain to try to construct tibble objects from their print output representations. And posting code to build examples is a specific suggestion in the Posting Guide.
--
David
>
> 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.
David Winsemius
Alameda, CA, USA
'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law
More information about the R-help
mailing list