[R] conditionally weighted mean with NAs
    Joshua Wiley 
    jwiley.psych at gmail.com
       
    Sun Apr  3 04:20:20 CEST 2011
    
    
  
Dear List,
I am trying to take the weighted average of two numbers (visual acuity
measures from the left and right eye).  For each row, the lowest value
should get a weight of .75, and the highest a weight of .25.  My
problem is, if one value is missing (NA), the remaining one should get
a weight of 1 (i.e., just return the nonmissing value), if both are
missing, NA should be returned.  Below is some example data and the
code I tried to write (Desired is what I actually want).  Any thoughts
or comments would be welcome.
Thanks,
Josh
VA <- cbind(OS = c(.2, 0, 1, -.1, NA, 3, NA),
  OD = c(.3, -.1, .2, -.1, NA, 0, .1),
  Desired = c(0.225, -0.075, 0.4, -0.1, NA, 0.75, 0.1))
## What I tried
weight.combine <- function(left, right) {
  out.r <- right
  out.l <- left
  right <- ifelse(out.r <= out.l, out.r * .75, out.r * .25)
  left <- ifelse(out.r > out.l, out.l * .75, out.l * .25)
  rowSums(cbind(left, right), na.rm = TRUE)
}
## This "works", except it does not handle NAs properly
weight.combine(VA[, "OS"], VA[, "OD"])
-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
    
    
More information about the R-help
mailing list