Skip to content
Back to formatted view

Raw Message

Message-ID: <1338484087020-4631974.post@n4.nabble.com>
Date: 2012-05-31T17:08:07Z
From: LCOG1
Subject: Probably a good use for apply
In-Reply-To: <4FC73B7B.2050201@bitwrit.com.au>

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.