[R] conditionally weighted mean with NAs
Joshua Wiley
jwiley.psych at gmail.com
Sun Apr 3 05:16:39 CEST 2011
On Sat, Apr 2, 2011 at 7:59 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Apr 2, 2011, at 10:20 PM, Joshua Wiley wrote:
>> 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))
>
> VA <- cbind(VA, 0.75*pmin(VA[, "OS"], VA[, "OD"], na.rm=TRUE) +
> 0.25*pmax(VA[, "OS"], VA[, "OD"], na.rm=TRUE) )
>
>> VA
> OS OD Desired
> [1,] 0.2 0.3 0.225 0.225
> [2,] 0.0 -0.1 -0.075 -0.075
> [3,] 1.0 0.2 0.400 0.400
> [4,] -0.1 -0.1 -0.100 -0.100
> [5,] NA NA NA NA
> [6,] 3.0 0.0 0.750 0.750
> [7,] NA 0.1 0.100 0.100
>
That works wonderfully, and it is so elegant! Thank you, David.
Josh
>
> David Winsemius, MD
> West Hartford, CT
>
More information about the R-help
mailing list