[R] repeated searching of no-missing values

hadley wickham h.wickham at gmail.com
Thu Dec 11 00:49:33 CET 2008


On Wed, Dec 10, 2008 at 4:09 PM, Patrizio Frederic
<frederic.patrizio at gmail.com> wrote:
> hi all,
> I have a data frame such as:
>
> 1 blue  0.3
> 1 NA    0.4
> 1 red   NA
> 2 blue  NA
> 2 green NA
> 2 blue  NA
> 3 red   0.5
> 3 blue  NA
> 3 NA    1.1
>
> I wish to find the last non-missing value in every 3ple: ie I want a 3
> by 3 data.frame such as:
>
> 1 red   0.4
> 2 blue  NA
> 3 blue  1.1
>
> I have written a little script
>
> data = structure(list(V1 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L
> ), V2 = structure(c(1L, NA, 3L, 1L, 2L, 1L, 3L, 1L, NA), .Label = c("blue",
> "green", "red"), class = "factor"), V3 = c(0.3, 0.4, NA, NA,
> NA, NA, 0.5, NA, 1.1)), .Names = c("V1", "V2", "V3"), class =
> "data.frame", row.names = c(NA,
> -9L))
>
> cl        = function(x) x[max(which(!is.na(x)))]

It's easily to do this with ddply from plyr:

library(plyr)
ddply(data, .(V1), colwise(cl))

In brief, this says to take the data frame called data and break it up
into pieces defined by the variable V1.  Then for each piece,
calculate cl for each column, and then join all the pieces back
together.

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list