# [R] creating tables with replacement

Marna Wagley marna.wagley at gmail.com
Wed Oct 18 18:54:39 CEST 2017

```Dear David and Petr,
It worked and I think now I can modify the code little bit to fit my
requirement. Thank you so much for your help.

Thanks,

MW

On Wed, Oct 18, 2017 at 7:15 AM, David L Carlson <dcarlson at tamu.edu> wrote:

> Building on Petr's suggestion, you could modify his code to get all 10
> samples at once in a compact format:
>
> > Samples <- lapply(lll, function(x) replicate(10, sample(x, rep=TRUE)))
> # Samples is a list containing 3 matrices, one for each group
> # Each column gives the index (row) numbers for a particular sample
> > str(Samples)
> List of 3
>  \$ A: int [1:3, 1:10] 2 1 3 1 1 3 2 3 1 2 ...
>  \$ B: int [1:7, 1:10] 8 5 8 6 5 8 10 6 4 7 ...
>  \$ C: int [1:9, 1:10] 14 15 19 13 18 19 19 12 13 16 ...
> # Row numbers for all 10 samples of 3 for group A
> > Samples\$A
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,]    2    1    2    2    3    3    2    3    2     3
> [2,]    1    1    3    1    3    3    2    3    3     3
> [3,]    3    3    1    2    2    1    2    3    3     1
> # The first sample from group A
> > dat1[Samples\$A[ , 1], ]
>   RegionA site temp group
> 2      Ra   s2   21     A
> 1      Ra   s1   23     A
> 3      Rb   s3   10     A
>
> # Save the first sample from group A as a .csv file
> > write.csv(dat1[Samples\$A[ , 1], ], row.names=FALSE, file="Test.csv")
>
> # Save all ten group A samples as a single .csv file
> # Faster but you have to split them into separate tables by hand
> > write.csv(dat1[Samples\$A[ , 1:10], ], row.names=FALSE, file="Test.csv")
>
> ----------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
>
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of PIKAL Petr
> Sent: Wednesday, October 18, 2017 3:38 AM
> To: Marna Wagley <marna.wagley at gmail.com>; r-help mailing list <
> r-help at r-project.org>
> Subject: Re: [R] creating tables with replacement
>
> Hi
>
> maybe there is another more elegant solution but something like this
>
> > idx <- 1:nrow(dat1)
> > lll <- split(idx, dat1\$group)
> > dat1[unlist(lapply(lll, sample, rep=TRUE)),]
>
> gives you selected rows.
>
> You could use for cycle or save those data frames manually
>
> Cheers
>
> Petr
> > -----Original Message-----
> > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Marna
> > Wagley
> > Sent: Wednesday, October 18, 2017 9:46 AM
> > To: r-help mailing list <r-help at r-project.org>
> > Subject: [R] creating tables with replacement
> >
> > Hi R User,
> > I am new in R and trying to create tables with selecting rows randomly
> > (but with replacement) for each group but each group should have same
> > number as original. Is it possible to create it using the following
> example data set?
> >
> > Your help is highly appreciated.
> >
> > dat1<-structure(list(RegionA = structure(c(1L, 1L, 2L, 3L, 3L, 4L,
> >
> > 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Ra",
> >
> > "Rb", "Rc", "Rd", "Re", "Rf"), class = "factor"), site =
> > structure(c(1L,
> >
> > 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L,
> >
> > 8L, 9L, 10L, 11L), .Label = c("s1", "s10", "s11", "s12", "s13",
> >
> > "s14", "s15", "s16", "s17", "s18", "s19", "s2", "s3", "s4", "s5",
> >
> > "s6", "s7", "s8", "s9"), class = "factor"), temp = c(23L, 21L,
> >
> > 10L, 15L, 16L, 8L, 13L, 1L, 23L, 19L, 25L, 19L, 12L, 16L, 19L,
> >
> > 21L, 12L, 5L, 7L), group = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
> >
> > 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A",
> >
> > "B", "C"), class = "factor")), .Names = c("RegionA", "site",
> >
> > "temp", "group"), class = "data.frame", row.names = c(NA, -19L
> >
> > ))
> >
> >
> > Here group A has 3 rows,
> >
> > B had 7 rows, and
> >
> > C has 9 rows
> >
> >
> > I want to select rows randomly (with replacement) for each group but
> > each group should have same number of rows as above (group A=3 rows,
> > B=7 rows, and
> > c=9 rows). This way I want to create at least 10 tables (10 random
> > tables) and save them separately in Excel.
> >
> >
> > Thanks
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ________________________________
> Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou
> Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě
> neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie
> vymažte ze svého systému.
> Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email
> jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
> Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi
> či zpožděním přenosu e-mailu.
>
> V případě, že je tento e-mail součástí obchodního jednání:
> - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření
> smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
> Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany
> příjemce s dodatkem či odchylkou.
> - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve
> výslovným dosažením shody na všech jejích náležitostech.
> - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za
> společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn
> nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto
> existence je adresátovi či osobě jím zastoupené známá.
>
> This e-mail and any documents attached to it may be confidential and are
> intended only for its intended recipients.
> If you received this e-mail by mistake, please immediately inform its
> sender. Delete the contents of this e-mail with all attachments and its
> If you are not the intended recipient of this e-mail, you are not
> authorized to use, disseminate, copy or disclose this e-mail in any manner.
> The sender of this e-mail shall not be liable for any possible damage
> caused by modifications of the e-mail or by delay with transfer of the
> email.
>
> In case that this e-mail forms part of business dealings:
> - the sender reserves the right to end negotiations about entering into a
> contract in any time, for any reason, and without stating any reasoning.
> - if the e-mail contains an offer, the recipient is entitled to
> immediately accept such offer; The sender of this e-mail (offer) excludes
> any acceptance of the offer on the part of the recipient containing any
> amendment or variation.
> - the sender insists on that the respective contract is concluded only
> upon an express mutual agreement on all its aspects.
> - the sender of this e-mail informs that he/she is not authorized to enter
> into any contracts on behalf of the company except for cases in which
> he/she is expressly authorized to do so in writing, and such authorization
> or power of attorney is submitted to the recipient or the person
> represented by the recipient, or the existence of such authorization is
> known to the recipient of the person represented by the recipient.
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help