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
> PLEASE do read the posting guide
> 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 ur?eny
pouze jeho adres?t?m.
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.
- a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout;
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 emailu p??padn?
osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich 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 copies from your
system.
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
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.