[R] Probably a good use for apply

LCOG1 jroll at lcog.org
Thu May 31 19:08:07 CEST 2012


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)))
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

--
View this message in context: http://r.789695.n4.nabble.com/Probably-a-good-use-for-apply-tp4631883p4631974.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list