Thanks for pointing that out, Gavin. I was trying to make the coding as
obvious as possible and was a little sloppy.
For completeness, the same thing without subset is...
foo - function(df,condition,...){
df. - df[eval(parse(text=condition)),]
idx - sample(1:nrow(df.),...)
df.[idx,]
}
Dear R users,
I’m trying to build a function to select random samples idf’s from a
database.
So, my data frame had 2 columns and 575 rows. Follow bellow an example of my
database
Idf1casod
12 1
14 1
15 1
16 1
17 3
18 3
19 3
21 3
25 1
24 1
26
A function, something like this?
randomSample(df,fraction=0.5){
fraction = min(max(0,fraction),1) #Just to check
nrows= nrow(df)
number = max(1,floor(fraction*nrows))
rowIds = sample(1:nrows,number)
df[rowIds,]
}
--
Nicholas Hamilton
School of Materials Science and Engineering
Hi,
If I understand you correctly, you want a function that lets you sample,
without replacement, from a data frame that is subsetted by a second
variable? Your question is a bit unclear, so this may be not what you want.
If it is, however, this should work:
# function that doesn't force you to
Sorry, I should say something like this (I am doing stuff in a few languages,
messed up the syntax in my last email) â¦.
randomSample - function(df,fraction=0.5){
fraction = min(max(0,fraction),1) #Just to check
nrows= nrow(df)
number = max(1,floor(fraction*nrows))
rowIds =
On 06/06/14 15:39, Rodrigues wrote:
Dear R users,
Iâm trying to build a function to select random samples idfâs from a
database.
So, my data frame had 2 columns and 575 rows. Follow bellow an example of my
database
Idf1 casod
121
141
151
161
173
183
19
As you are trying to learning to write functions, I'll submit a very
general solution
that illustrates a couple of core skills. It returns a random sample of
rows for
any dataframe and any condition, and accepts optional arguments to sample.
#Example data
Idf1 -
I don't think it is good general advice to suggest that people use
`subset()` in a function. You have to be every so clever to make this work
right once you call `foo()` inside another function because of where
`eval(cond)` is doing its evaluation of the `condition` won't be the same
as when you
Hello,
I would like to thanks everyone for the help.
I did this one:
blinding=function(sampling){
sort1=sample(idf[casod==1],30,replace=F)
sort2=sample(idf[casod==3],30,replace=F)
var1=factor(c(rep(1,30),rep(2,30)),labels=c(Positivo,Negativo))
var2=cbind(c(pos=sort1,neg=sort2))