On Fri, 2 Sep 2016, Bert Gunter wrote:
[snip]

The "trick" is to use tapply() to select the necessary row indices of
your data frame and forget about all the do.call and rbind stuff. e.g.


I agree the way to go is "select the necessary row indices" but I get there a different way. See below.

set.seed(1001)
df <- data.frame(f =factor(sample(LETTERS[1:4],100,rep=TRUE)),
+                  g <- factor(sample(letters[1:6],100,rep=TRUE)),
+                  y = runif(100))

ix <- seq_len(nrow(df))

ix <- with(df,tapply(ix,list(f,g),function(x)x[length(x)]))
ix
  a  b   c  d  e  f
A 94 69 100 59 80 87
B 89 57  65 90 75 88
C 85 92  86 95 97 62
D 47 73  72 74 99 96


  jx <- which( !duplicated( df[,c("f","g")], fromLast=TRUE ))

  xtabs(jx~f+g,df[jx,]) ## Show equivalence to Bert's `ix'

   g
f     a   b   c   d   e   f
  A  94  69 100  59  80  87
  B  89  57  65  90  75  88
  C  85  92  86  95  97  62
  D  47  73  72  74  99  96


Chuck

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to