Skip to content

piece wise application of functions

3 messages · Itay Furman, Tom Blackwell

#
Dear all,

After struggling for some time with *apply() and eva() without
success, I decided to ask for help.

I have 3 lists labeled with, each contains 3 different
interpolation functions with identical names:
[1] "spl.1mb" "spl.2mb" "spl.5mb"
[1] "spl.1mb" "spl.2mb" "spl.5mb"
[1] "spl.1mb" "spl.2mb" "spl.5mb"
(
In case it matters the functions accept and return one argument:
block.size <- spl.1mb(ic)
)

Then, I have 2 data frames with identical structure:
intvl.mb    ic
1    1e+06 0.597
2    2e+06 0.504
3    5e+06 0.327
4    1e+07 0.204
intvl.mb      ic
1    1e+06 0.67200
2    2e+06 0.62325
3    5e+06 0.51000
4    1e+07 0.38775
I would like to apply the functions on these data frames
piece-wise and create a data frame per function _list_.

So I am looking for a final output like this:
gap	 snps	strs
1 1e+06  ..	..
2 2e+06  ..	..
3 5e+06  ..	..

Here, case0$snps[1] is, for example, the result of applying the
function in  missgp0[1] on the entry snps.missgp$ic[1];
and, case0$strs[1] is the result of applying the same function
on strs.missgp$ic[1].

Then, I want to repeat the whole thing with missgp1,2  instead
of missgp0, generating case1,2 data frames.

How should I do it?


	Thanks in advance,
	Itay Furman

--------------------------------------------------------------------
itayf at fhcrc.org			Fred Hutchinson Cancer Research Center
#
Itay  -

If it were my problem, I would re-structure the task around the
existing capabilities of  lapply().  In particular, I would
concatenate the three lists of functions, then write a wrapper
function which takes three arguments:  an index, a list of
functions and a data set.  Then I would call  lapply()  twice
to get the result you appear to be asking for.  Here's a rough
example, untested, which may not work exactly for your situation.

miss.all <- c(missgp0, missgp1, missgp2)
wrapper  <- function(i, ff, d)  {ff[[i]](d)}
result.1 <- unlist(lapply(seq(9), wrapper, miss.all, snps.missgp[["ic"]]))
result.2 <- unlist(lapply(seq(9), wrapper, miss.all, strs.missgp[["ic"]]))
gaps     <- c(  #  nine multiples of 1e+6 which describe the nine functions )
result   <- cbind(gaps=gaps, snps=result.1, strs=result.2)

In this case,  result  is a matrix, not a data frame, but you can
easily convert between the two.

HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -
On Thu, 19 Feb 2004, Itay Furman wrote:

            
#
Thanks!  Especially for pointing out the usage of a 'wrapper' 
function in conjunction to lapply. In addition, data 
re-organization was important, too, as you pointed out.

My final solution was slightly different than your proposition.
See below.

	Thanks again,
	Itay
On Fri, 20 Feb 2004, Tom Blackwell wrote:

            
# Make skeleton d.f. for the results
gaps <- data.frame(gap=paste(c(1, 2, 5), "+06", sep="e")
# Re-organize data :-)
# Each interpolation function will operate on a single row
markers <- ( markers <- cbind(strs.missgp[["ic"]], 
			      snps.missgp[["ic"]]) )[1:3,]
# A slight different definition: makes explicit the application 
# to rows
wrapper <- function(i, ff, d) {ff[[i]](d[i,])}

# Compute. Note the use of sapply()
case0 <- cbind(gaps, t(sapply(seq(3), wrapper, missgp0, 
		markers)))
case1 <- cbind(gaps, t(sapply(seq(3), wrapper, missgp1, 
		markers)))
case2 <- cbind(gaps, t(sapply(seq(3), wrapper, missgp2, 
		markers)))

gaps was a d.f.; therefore, cbind() coerces the result to d.f.
For example:
case0
    gap      strs     snps
1 1e+06 46145.218 374.3882
2 2e+06  2547.841 494.0718
3 5e+06  1372.235 402.9667
I tried to loop over the indices of case and missgp, 0,1,2,
using assign() but some how failed; and really wanted to go on 
with the analysis.

Later, it occured to me that I might have used some wrapper 
function in combination with outer(); and the result would be in 
the diag()onal. That is because what in fact I was looking for 
was an inner prodcut of a vector of functions, with a vector of 
arguments.

	Thanks again,
	Itay