[R] sapply function and poisson distribution

Pete Brecknock
Sun, 04 Jan 2015 00:45:33 -0000

dimnik wrote
> i want to find  a function	that	takes	in two vectors of	numbers	that	have
> the	same
> length.The output should be a list	of vectors,	where each	vector is a
> sequence	of
> randomly	generated	Poisson variables	 where the	number of	samples in	each
> vector is determined by the entries in the first	input	vector and	the
> lambdas	come
> from	the	entries in the second input vector.	For example, :If the inputs
> are c(1,2)	and	c(0.1,0.8)	the output	will be a list of twovectors where
> the first	vectorhas	a	single	sample	from	Poisson(0.1) and	the second
> vector has	two	samples from Poisson(0.8).How can i do all that kind of
> stuff using sapply function?
> thank u in advance

How about using mapply, the multivariate version of sapply?

Based on your example ...

mapply(function(x,y) rpois(x,y), c(1,2),c(0.1,0.8))

HTH

Pete