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

R: using a for loop to create a new data table containing min and max variables given multiple column combinations

$
0
0

I am currently working with a data set in R that contains four variables for a large set of individuals: pid, month, window, and agedays. I'm trying to create a loop that will output the min and max agedays of each group of combinations between month and window into a new data table that I can export as a csv.

Here's an example of the data:

    pid    agedays    month    window
    1      22         2        1
    2      35         3        2
    3      33         3        2
    4      55         3        2
    1      66         2        1
    2      55         4        2
    3      80         4        2
    4      90         4        2

I'd like for the new data table to contain the min and max agedays of each group within each combination of window and month as well as the count of each group within each combination. The range for month is 2-24 and the range for window is 0-2.

The data table should look something like this:

    month    window    min    max    N
    2        1         22     66     1
    3        2         33     55     3

etc....

where N is the number of unique individuals (pids) within each group


Viewing all articles
Browse latest Browse all 202012

Trending Articles



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