Selecting values from a 3-column dataframe in R -
i have 3-dimensional array, variables being x, y , z. x list of places, y list of time, , z list of names. list of names not start @ same initial time across places:
x y z x1 1 na x1 2 z2 x1 3 z3 x1 4 z1 x2 1 na x2 2 na x2 3 z5 x2 4 z3 x3 1 z3 x3 2 z1 x3 3 z2 x3 4 z2 how find first z every x? want output matrix or dataframe be:
x z x1 z2 x2 z5 x3 z3
edited, after example data supplied
you can use function ddply() in package plyr
dat <- "x y z x1 1 na x1 2 z2 x1 3 z3 x1 4 z1 x2 1 na x2 2 na x2 3 z5 x2 4 z3 x3 1 z3 x3 2 z1 x3 3 z2 x3 4 z2" df <- read.table(textconnection(dat), header=true, stringsasfactors=false) library(plyr) ddply(df, .(x), function(x)x[!is.na(x$z), ][1, "z"]) x v1 1 x1 z2 2 x2 z5 3 x3 z3
Comments
Post a Comment