Probably a good use for apply
Hi,
On Thu, May 31, 2012 at 1:08 PM, LCOG1 <jroll at lcog.org> wrote:
This is great thank you. ?I think I am getting the hang of some of the apply functions. ?I am stuck again however. ?I have list test_ below and would like to apply the sample function using each element of each vector as the probability and return a TRUE or FALSE that I will ultimately sum the TRUES by vector. test_<- list(a=c(.85,.10),b=c(.99,.05)) #Write a function to sample based on labor force participation rates to determine presence of workers in household sampleWorker <- function(x) return(sample(c(TRUE,FALSE),x, replace = TRUE, prob = c(x, 1-x)))
Your first problem is that sampleWorker() doesn't run with a single component of test_ so it can't possibly run in an apply statement. Please reread ?sample - the second argument is the size of the desired sample, but what you are passing is a non-integer vector of length 2. What do you actually want this to be? Then for prob, you're passing c(x, 1-x)) but x is again a non-integer vector of length 2, so that results in a vector of length 4, which is longer than the number of options sample() is choosing from. Do you perhaps want to pass only a single probability at a time? But even then you need to resolve the size problem. Sarah
IsWorker.Hh_ <- lapply(test , sampleWorker) I am doing something wrong with the setup becuase i am getting an error about specifying probabilities incorrectly. The result I am looking for for ?IsWorker_ to be (assuming the .85, and . 99 probabilities 'win' from each vector and the lower values do not.
IsWorker_
$a [1]TRUE $b [1]TRUE but ultimately I will need to sum the TRUEs for each vector
IsWorker_
$a [1] 1 $b [1] 1 Thanks Josh
Sarah Goslee http://www.functionaldiversity.org