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: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Andrew Anglemyer
Sent: Wednesday, 28 July 2010 9:22 AM
To: r-help at r-project.org
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]]
______________________________________________
R-help at r-project.org 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.