Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

R: How to carry the last observed value forward, within the same unit of observation

$
0
0

I have a little problem with R and the tidyverse that I unfortunately can't seem to solve.

Say we have quarterly observations of three shops (in the minimum working example the values really don't matter, what matters is the NA's and the few postcodes in the last column).

library(tidyverse)



mydf <- tribble(~shop, ~quarter, ~revenue, ~postcode,  "Shop A",1, 100, NA, "Shop A",2, 210, NA,"Shop A",3, 50 , NA,  "Shop A",4, 100, 1000,  "Shop B", 1, 40, NA,  "Shop B", 2, 80, NA,   "Shop B", 3, 20, NA,   "Shop B", 4, 40, NA,  "Shop C", 1, 20, 2011,  "Shop C", 2, 20, NA,  "Shop C", 3, 30, NA,  "Shop C", 4, 25, NA,)

Shop A only has a single Postcode in the fourth quarter. Shop B doesn't have any postcode, and the Shop C only has a Postcode in the first quarter, see mydf.

We can safely assume that postcodes for the shops don't change, so we can carry forward the last observed value, i.e. postcode.

I use tidyverse for this, the command is as follows.

mydf %>% fill(postcode, .direction="down")
mydf %>% fill(postcode, .direction="up")

Unfortunately, R doesn't seem to care very much about the units of observations, it just pick the postcodes it finds and carries this value forward, in this example across different shops.

What I would like to end up with, is the postcodes filled for Shops A and C, and not for B for which I would have to manually lookup the postcode.

( in the dataset i work with, there are thousands of obervations("shops") so this would be a good way wo minimize workload.)

I am pretty new to R, so please don't be to harsh on me.

Thanks in advance for any solution hints.

David


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>