Hello everyone, I have a data set like this:> head( fish_transect)ID_TRANSECT ID_PROJECT DE_ZONE DE_LOCALITY DE_SECTOR MES 1 42 MB Tarragona Creixell Control I 9 2 42 MB Tarragona Creixell Control I 9 3 42 MB Tarragona Creixell Control I 9 4 42 MB Tarragona Creixell Control I 9 5 42 MB Tarragona Creixell Control I 9 6 42 MB Tarragona Creixell Control I 9 ID_SPECIES WEIGHT SIZE N FAMILIA 1 Spondyliosoma cantharus 15.64 10 1 Sparidae 2 Symphodus melanocercus 11.21 10 1 Labridae 3 Diplodus vulgaris 30.20 10 2 Sparidae 4 Diplodus vulgaris 52.24 12 2 Sparidae 5 Diplodus sargus 221.41 14 5 Sparidae 6 Diplodus annularis 3.47 6 1 Sparidae I have been trying to duplicate the rows where N> 1, that is I want a row for each animal. Right now as you can see I have for example 5 D. sargus which are 14 cm length and so on. How can I get 1 row for each animal? I've been trying the reshape function without success. I also tried in access but couldn't do it either. Thanks a lot, Lucia -- View this message in context: http://r.789695.n4.nabble.com/reshape-data-adding-rows-to-dataframe-tp3253640p3253640.html Sent from the R help mailing list archive at Nabble.com.
Hi, Not sure what you want to do, but take a look at ?duplicated, ?unique, and maybe ?aggregate; it might be what you're looking for. HTH, Ivan Le 2/2/2011 10:13, Lucia Rueda a ?crit :> Hello everyone, > > I have a data set like this: > >> head( fish_transect) > ID_TRANSECT ID_PROJECT DE_ZONE DE_LOCALITY DE_SECTOR MES > 1 42 MB Tarragona Creixell Control I 9 > 2 42 MB Tarragona Creixell Control I 9 > 3 42 MB Tarragona Creixell Control I 9 > 4 42 MB Tarragona Creixell Control I 9 > 5 42 MB Tarragona Creixell Control I 9 > 6 42 MB Tarragona Creixell Control I 9 > ID_SPECIES WEIGHT SIZE N FAMILIA > 1 Spondyliosoma cantharus 15.64 10 1 Sparidae > 2 Symphodus melanocercus 11.21 10 1 Labridae > 3 Diplodus vulgaris 30.20 10 2 Sparidae > 4 Diplodus vulgaris 52.24 12 2 Sparidae > 5 Diplodus sargus 221.41 14 5 Sparidae > 6 Diplodus annularis 3.47 6 1 Sparidae > > I have been trying to duplicate the rows where N> 1, that is I want a row > for each animal. Right now as you can see I have for example 5 D. sargus > which are 14 cm length and so on. How can I get 1 row for each animal? I've > been trying the reshape function without success. I also tried in access but > couldn't do it either. > > Thanks a lot, > > Lucia > > >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
On Wed, Feb 02, 2011 at 01:13:11AM -0800, Lucia Rueda wrote:> > Hello everyone, > > I have a data set like this: > > > head( fish_transect) > ID_TRANSECT ID_PROJECT DE_ZONE DE_LOCALITY DE_SECTOR MES > 1 42 MB Tarragona Creixell Control I 9 > 2 42 MB Tarragona Creixell Control I 9 > 3 42 MB Tarragona Creixell Control I 9 > 4 42 MB Tarragona Creixell Control I 9 > 5 42 MB Tarragona Creixell Control I 9 > 6 42 MB Tarragona Creixell Control I 9 > ID_SPECIES WEIGHT SIZE N FAMILIA > 1 Spondyliosoma cantharus 15.64 10 1 Sparidae > 2 Symphodus melanocercus 11.21 10 1 Labridae > 3 Diplodus vulgaris 30.20 10 2 Sparidae > 4 Diplodus vulgaris 52.24 12 2 Sparidae > 5 Diplodus sargus 221.41 14 5 Sparidae > 6 Diplodus annularis 3.47 6 1 Sparidae > > I have been trying to duplicate the rows where N> 1, that is I want a row > for each animal. Right now as you can see I have for example 5 D. sargus > which are 14 cm length and so on. How can I get 1 row for each animal? I've > been trying the reshape function without success. I also tried in access but > couldn't do it either.Hello. Let me use a simpler example for testing. dat <- data.frame(animal=c("a", "b", "c"), N=c(2, 1, 3)) dat animal N 1 a 2 2 b 1 3 c 3 Is the following operation what you want in terms of this small example? expand <- dat[rep(1:nrow(dat), times=dat$N), ] rownames(expand) <- NULL expand animal N 1 a 2 2 a 2 3 b 1 4 c 3 5 c 3 6 c 3 Hope this helps. Petr Savicky.
Exactly!! Thanks a lot Petr. It worked! Thansk to you as well Ivan! -- View this message in context: http://r.789695.n4.nabble.com/reshape-data-adding-rows-to-dataframe-tp3253640p3253793.html Sent from the R help mailing list archive at Nabble.com.