repeating rows in R

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

repeating rows in R

Andrew Anglemyer
I'm somewhat a new user and have been trying to figure out how to repeat
rows a certain number of time based on a variable.  Currently, the number of
rows is not reflective of the number of observations.  To get the number of
observations (n=22 in this case), I have to multiply by the variable
NoRecords for each row.  So, there are actually 7 cases of cancer and 3
cases of HIV in my data, not 2 cases of HIV and 1 case of cancer.  Is there
an easy way to expand my data so that I actually end up with 22 rows instead
of 10?  Specifically, my data currently look like this:

>my data
   AdmitYear Race Age.yrs. Insurance       HIV   Cancer NoRecords
1       1985    A       20              0                  0
0         1
2       1985    A       21              0                  0
0         1
3       1985    A       22              1                  1
0         1
4       1985    A       23              0                  0
0         2
5       1985    A       24              0                  1
0         2
6       1985    A       24              1                  0
0         1
7       1985    A       25              1                  0
0         3
8       1985    A       26              0                  0
0         2
9       1985    A       26              1                  0           1
        7
10      1985    A       27             0                  0
0         2

thanks!
Andy

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: repeating rows in R

Bill.Venables
Is this the kind of thing you are looking for?

> dat <- data.frame(x = 1:3, freq = 2:4)
> dat
  x freq
1 1    2
2 2    3
3 3    4
> newDat <- dat[rep(rownames(dat), dat$freq), ]
> newDat
    x freq
1   1    2
1.1 1    2
2   2    3
2.1 2    3
2.2 2    3
3   3    4
3.1 3    4
3.2 3    4
3.3 3    4
>  

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Andrew Anglemyer
Sent: Wednesday, 28 July 2010 9:22 AM
To: [hidden email]
Subject: [R] repeating rows in R

I'm somewhat a new user and have been trying to figure out how to repeat
rows a certain number of time based on a variable.  Currently, the number of
rows is not reflective of the number of observations.  To get the number of
observations (n=22 in this case), I have to multiply by the variable
NoRecords for each row.  So, there are actually 7 cases of cancer and 3
cases of HIV in my data, not 2 cases of HIV and 1 case of cancer.  Is there
an easy way to expand my data so that I actually end up with 22 rows instead
of 10?  Specifically, my data currently look like this:

>my data
   AdmitYear Race Age.yrs. Insurance       HIV   Cancer NoRecords
1       1985    A       20              0                  0
0         1
2       1985    A       21              0                  0
0         1
3       1985    A       22              1                  1
0         1
4       1985    A       23              0                  0
0         2
5       1985    A       24              0                  1
0         2
6       1985    A       24              1                  0
0         1
7       1985    A       25              1                  0
0         3
8       1985    A       26              0                  0
0         2
9       1985    A       26              1                  0           1
        7
10      1985    A       27             0                  0
0         2

thanks!
Andy

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: repeating rows in R

Andrew Anglemyer
Thanks!

On Tue, Jul 27, 2010 at 4:29 PM, <[hidden email]> wrote:

> Is this the kind of thing you are looking for?
>
> > dat <- data.frame(x = 1:3, freq = 2:4)
> > dat
>  x freq
> 1 1    2
> 2 2    3
> 3 3    4
> > newDat <- dat[rep(rownames(dat), dat$freq), ]
> > newDat
>    x freq
> 1   1    2
> 1.1 1    2
> 2   2    3
> 2.1 2    3
> 2.2 2    3
> 3   3    4
> 3.1 3    4
> 3.2 3    4
> 3.3 3    4
> >
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Andrew Anglemyer
> Sent: Wednesday, 28 July 2010 9:22 AM
> To: [hidden email]
> Subject: [R] repeating rows in R
>
> I'm somewhat a new user and have been trying to figure out how to repeat
> rows a certain number of time based on a variable.  Currently, the number
> of
> rows is not reflective of the number of observations.  To get the number of
> observations (n=22 in this case), I have to multiply by the variable
> NoRecords for each row.  So, there are actually 7 cases of cancer and 3
> cases of HIV in my data, not 2 cases of HIV and 1 case of cancer.  Is there
> an easy way to expand my data so that I actually end up with 22 rows
> instead
> of 10?  Specifically, my data currently look like this:
>
> >my data
>   AdmitYear Race Age.yrs. Insurance       HIV   Cancer NoRecords
> 1       1985    A       20              0                  0
> 0         1
> 2       1985    A       21              0                  0
> 0         1
> 3       1985    A       22              1                  1
> 0         1
> 4       1985    A       23              0                  0
> 0         2
> 5       1985    A       24              0                  1
> 0         2
> 6       1985    A       24              1                  0
> 0         1
> 7       1985    A       25              1                  0
> 0         3
> 8       1985    A       26              0                  0
> 0         2
> 9       1985    A       26              1                  0           1
>        7
> 10      1985    A       27             0                  0
> 0         2
>
> thanks!
> Andy
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
*************************************************
Andrew Anglemyer, PhD MPH
Methods and Statistics Editor
Cochrane HIV/AIDS Group
Institute for Global Health
University of California, SF

Research Postdoctoral Fellow
Department of Pediatrics, Infectious Diseases
Stanford University
Stanford, CA 94305

email:  [hidden email]
ph:  +1.510.717.3029

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.