Hi, 

you can try

df1<-split(df,df$groups) 

lapply(df1, function(x) 
{ 
 x<-cbind(x,entry=0) 
 sam <- sample(x$plotno,1) 
 x$entry[which(x$plotno==sam)]<-"CONTROL" 
 x$entry[which(!x$plotno==sam)]<-"TEST" 
 x 
} 
)

 
Tanvir Ahamed 
Göteborg, Sweden  |  mashra...@yahoo.com 



________________________________
From: "DIGHE, NILESH [AG/2362]" <nilesh.di...@monsanto.com>
To: "r-help@r-project.org" <r-help@r-project.org> 
Sent: Thursday, 17 March 2016, 18:18
Subject: [R] sample within a loop


Dear R users,
                My data frame has four "groups" namely A1, B2, C3, & D4.  Each 
group has 12 rows (variable "plotno).  I like to randomly sample one "plotno" 
within each "groups" variable and label it as "CONTROL" and label others as 
"TEST" in a new variable called "entry".  I am trying to do this by looping 
over the group variable and then sample "plotno" within a given group.  I am 
ending up with four "CONTROL" plots but they are generated by sampling over all 
the groups instead of each group.  I need one random "plotno" assigned as a 
"CONTROL" per group (A1, B2, C3, D4).  I would appreciate any help in modifying 
my function "funa" or suggest any alternative and better way to do this task.  
Below is the dataset and function I am working with.

# dataset (df)
structure(list(plotno = 1:48, groups = c("A1", "A1", "A1", "A1",
"A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "B2", "B2", "B2",
"B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "C3", "C3",
"C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "D4",
"D4", "D4", "D4", "D4", "D4", "D4", "D4", "D4", "D4", "D4", "D4"
)), .Names = c("plotno", "groups"), row.names = c(NA, -48L), class = 
"data.frame")

# function (funa)

function (dataset)

{

    set.seed(1)

    bay <- unique(dataset$groups)

    IND <- c()

    df2 <- dataset

    for (i in bay) {

        IND[i] <- which(plotno %in% sample(plotno, 1))

        df2$entry <- ifelse(df2$plotno %in% IND, "CONTROL", "TEST")

    }

    df2

}


# session info

R version 3.2.1 (2015-06-18)

Platform: i386-w64-mingw32/i386 (32-bit)

Running under: Windows 7 x64 (build 7601) Service Pack 1



locale:

[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252  
  LC_MONETARY=English_United States.1252

[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252



attached base packages:

[1] stats     graphics  grDevices utils     datasets  methods   base



loaded via a namespace (and not attached):

[1] tools_3.2.1

Thanks.
Nilesh


Nilesh Dighe
(806)-252-7492 (Cell)
(806)-741-2019 (Office)


This e-mail message may contain privileged and/or confidential information, and 
is intended to be received only by persons entitled
to receive such information. If you have received this e-mail in error, please 
notify the sender immediately. Please delete it and
all attachments from any servers, hard drives or any other media. Other use of 
this e-mail by you is strictly prohibited.

All e-mails and attachments sent and received are subject to monitoring, 
reading and archival by Monsanto, including its
subsidiaries. The recipient of this e-mail is solely responsible for checking 
for the presence of "Viruses" or other "Malware".
Monsanto, along with its subsidiaries, accepts no liability for any damage 
caused by any such code transmitted by or accompanying
this e-mail or any attachment.


The information contained in this email may be subject to the export control 
laws and regulations of the United States, potentially
including but not limited to the Export Administration Regulations (EAR) and 
sanctions regulations issued by the U.S. Department of
Treasury, Office of Foreign Asset Controls (OFAC).  As a recipient of this 
information you are obligated to comply with all
applicable U.S. export laws and regulations.

    [[alternative HTML version deleted]]

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to