Skip to content
Prev 44626 / 398506 Next

Solved: [R] piece wise application of functions

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