lp@r-project.org
Subject: Re: [R] sample within a loop
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
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 tr
org, Sweden | mashra...@yahoo.com
From: "DIGHE, NILESH [AG/2362]"
To: "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,
I would change strategies.
Create a new variable, say,
num.in.grp <- rep(1:12, 4)
Then sample from 1:12, and add appropriate amounts so that they become row
numbers within the four sets of 12 rows
ctrls <- ssample(1:12, 4, replace=TRUE) + c(0,12,24,36)
Now that we have four random row number
4 matches
Mail list logo