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

Looping to restructure data for time series

$
0
0

I have a data frame that I need to restructure for a time series analysis. I have sales data for ITEMS where the date is a YEAR_MONTH value (e.g., 2014_06). I don't care about the date, however; what I really care about is if it's the 1st, 2nd, Nth month of sales. The problem is that different items had their first month of sales at different times, so my data frame looks something like this:

ITEMS  YEAR_MONTH  QUANTITY
111      2014_01      0
111      2014_02      12
111      2014_03      7
111      2014_04      1
132      2014_01      0
132      2014_02      0
132      2014_03      6
132      2014_04      6

What I need instead is a data frame that contains rows starting with the first YEAR_MONTH of non-zero sales, and labels them by order (e.g., MONTH_COUNTER 1, 2, 3, N). The sample data frame from above would be:

ITEMS  MONTH_COUNTER  QUANTITY
111          1           12
111          2           7
111          3           1
132          1           6
132          2           6

It seems like a relatively straightforward looping task, but it's far beyond my expertise at this point and I've struggled to find an answer elsewhere.


Viewing all articles
Browse latest Browse all 205491

Trending Articles



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