Re: [R] Repeating lines in a data frame

From: jim holtman <jholtman_at_gmail.com>
Date: Wed 19 Oct 2005 - 06:48:51 EST

Try this:
 > x
year species length count
1 1998 1 150 1
2 1998 2 200 1
3 1998 3 250 2
4 1999 1 150 3
5 1999 2 200 4
6 1999 3 250 5
7 2000 1 150 1
8 2000 2 200 1
9 2000 3 250 1
10 2001 1 150 2
11 2001 2 200 3
12 2001 3 250 1
13 2002 1 150 1
14 2002 2 200 2
15 2002 3 250 3
> y <- unlist(lapply(seq(nrow(x)), function(.row)rep(.row, x$count[.row])))
# replicate the row numbers
> y

[1] 1 2 3 3 4 4 4 5 5 5 5 6 6 6 6 6 7 8 9 10 10 11 11 11 12 13 14 14 15 15 15
> result <- x[y,] # pick out the rows
> result$count <- 1 # set the count to 1
> result


year species length count
1 1998 1 150 1
2 1998 2 200 1
3 1998 3 250 1
3.1 1998 3 250 1
4 1999 1 150 1
4.1 1999 1 150 1
4.2 1999 1 150 1
5 1999 2 200 1

5.1 1999 2 200 1
5.2 1999 2 200 1
5.3 1999 2 200 1

6 1999 3 250 1
6.1 1999 3 250 1
6.2 1999 3 250 1
6.3 1999 3 250 1
6.4 1999 3 250 1

7 2000 1 150 1
8 2000 2 200 1
9 2000 3 250 1
10 2001 1 150 1
10.1 2001 1 150 1
11 2001 2 200 1
11.1 2001 2 200 1
11.2 2001 2 200 1
12 2001 3 250 1
13 2002 1 150 1
14 2002 2 200 1
14.1 2002 2 200 1
15 2002 3 250 1
15.1 2002 3 250 1
15.2 2002 3 250 1
>

 On 10/18/05, Guenther, Cameron <Cameron.Guenther@myfwc.com> wrote:
>
> Hello,
> I have a much larger dataset that is similar in form to:
> year species length count
> 1998 1 150 1
> 1998 2 200 1
> 1998 3 250 2
> 1999 1 150 3
> 1999 2 200 4
> 1999 3 250 5
> 2000 1 150 1
> 2000 2 200 1
> 2000 3 250 1
> 2001 1 150 2
> 2001 2 200 3
> 2001 3 250 1
> 2002 1 150 1
> 2002 2 200 2
> 2002 3 250 3
>
> What I want is to have a line of data for each year x species x length
> group combination
> I would like the ouput to be:
>
> Year species length count
> 1998 1 150 1
> 1998 2 200 1
> 1998 3 250 1
> 1998 3 250 1
> 1999 1 150 1
> 1999 1 150 1
> 1999 1 150 1
> 1999 2 200 1
> .
> .
> .
>
> Can anyone help me with a for statement of a function that can
> accomplish this?
> Thanks

>
> Cameron Guenther
> Associate Research Scientist
> FWC/FWRI, Marine Fisheries Research
> 100 8th Avenue S.E.
> St. Petersburg, FL 33701
> (727)896-8626 Ext. 4305
> cameron.guenther@myfwc.com
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Oct 19 06:54:00 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:03:24 EST