1

I have a data frame in R Studio that has 3 columns: Year, Office, Market Share. I would like to create a new data frame that has the following columns so I may do a regression and see if Market Share is stable year over year:

Year / YearN / YearN+1 / Office

The data frame appears as this:

Year / Office / Market Share

data frame and is 5 years worth of data.

I have tried the unlist function, however was unsuccessful. I am open to suggestions.

Apologies this post might be confusing, first SO post :/

  • If you're looking to do a regression wouldn't you want to keep it in it's current format? – James B Jul 29 '19 at 20:30
  • Please provide [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and code examples – Trenton McKinney Jul 29 '19 at 22:08

1 Answers1

0

This would be easier with a reproducible example. So apologies if I'm not quite understanding what you're going for here.

One easy way to do this is in dplyr with lag:

library(dplyr)
df <- data.frame(year = c(2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015),
             retailer = c("a", "a", "a", "b", "b", "b", "c", "c", "c"),
             market_share = c(8, 25, 32, 16, 12, 11, 34, 89, 17)) 
new_df <- df %>% 
  group_by(retailer) %>% 
  mutate(year_ago = lag(market_share, 1))
df

   year retailer market_share year_ago
  <dbl> <fct>           <dbl>    <dbl>
1  2013 a                   8       NA
2  2013 a                  25        8
3  2013 a                  32       25
4  2014 b                  16       NA
5  2014 b                  12       16
6  2014 b                  11       12
7  2015 c                  34       NA
8  2015 c                  89       34
9  2015 c                  17       89

If this isn't what you're looking for, feel free to update your post with an example and I'm happy to take another crack.

benc
  • 376
  • 1
  • 6