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 always have the same column titles

myfunc = function(my_df,n,var1='idf',var2='casod',var2_subset = 2){
  data_of_interest = my_df[my_df[,var2] %in% var2_subset,]
  result = sample(my_df[,var1],n,replace=FALSE)
}

#test data
my_df = data.frame(idf=rnorm(100),casod=sample(c(1,2,3),100,replace=TRUE))

# here, it will return 10 samples from the idf rows where casod == 2
x = myfunc(my_df,10,'idf','casod',2)

Jeremy


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
>



-- 

*___________________________________________________________________________Jeremy
M. Chacon, Ph.D.*

*Post-Doctoral Associate, Gardner Lab*
*University of Minnesota*
*Genetics, Cell Biology, and Development*
*6-160 Jackson Hall*
*321 Church St. SE*
*Minneapolis, MN 55455*
*USA*

*[email protected] <[email protected]>*
*Lab telephone: **612-626-6906*

        [[alternative HTML version deleted]]

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

Reply via email to