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 <- c(12,14,15,16,17,18,19,21,25,24,26,28,29,32,33,35,36,37,48)
casod <- c(1,1,1,1,3,3,3,3,1,1,1,1,3,3,3,1,3,1,3)
mydatabase <- data.frame(Idf1,casod)

#Required arguments are `df', a dataframe, and `condition',
#a character string.  The function also accepts  optional
#arguments to `sample'

foo <- function(df,condition,...){
  cond <- parse(text=condition)
  df. <- subset(df,eval(cond))
  idx <- sample(1:nrow(df.),...)
  df.[idx,])
}

foo(df=mydatabase,condition="casod!=1",size=5,replace=FALSE)
foo(df=mydatabase,condition="casod!=1",size=10,replace=TRUE)








On Fri, Jun 6, 2014 at 8:39 AM, Rodrigues <[email protected]> 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
> 12      1
> 14      1
> 15      1
> 16      1
> 17      3
> 18      3
> 19      3
> 21      3
> 25      1
> 24      1
> 26      1
> 28      1
> 29      3
> 32      3
> 33      3
> 35      1
> 36      3
> 37      1
> 48      3
>
> So my function is
>
> blinding=function(sample){
>   sort=sample(idf1,10,replace=F)
>   return(sort2)
> }
>
> It is pretty simple and I would like to add one more step in my choice. I
> would like to link my choice to casod stats. Thus if casod==3 sample would
> be random idfs could not be an idf with casod=1. Does someone can help me?
>
>
>
>
> --
> View this message in context:
> http://r-sig-ecology.471788.n2.nabble.com/Help-with-a-function-tp7578931.html
> Sent from the r-sig-ecology mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-ecology mailing list
> [email protected]
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>



-- 
Glen Sargeant, Ph.D.
Research Wildlife Biologist/Statistician
USGS Northern Prairie Wildlife Research Center
E-mail: [email protected]
Phone: (701) 253-5528

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

Reply via email to