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

R: collapse rows using two ID variables (one ID nested in the other) [duplicate]

$
0
0

I have a data frame like this:

user_id    year   
A          2011
A          2012
A          2012
A          2012
A          2013
A          2013
B          2011
B          2012
B          2012
B          2013
B          2013
B          2013
.
.
.

I would like to collapse this data frame by using user_id and year, to create a new column count indicating the number of the appearance of that year. For example, for user A, there are one row of 2011, three rows of 2012, and two rows of 2013. So the value of count for these three years would be 1, 3, 2, respectively. The data frame would be collapsed into something like this:

user_id    year   count
A          2011     1
A          2012     3
A          2013     2
B          2011     1
B          2012     2
B          2013     3
.
.
.

I have no clue how to leverage R to accomplish this...Any help would be much appreciated.

Many thanks!

-Ian


Viewing all articles
Browse latest Browse all 206316

Trending Articles



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