On Jul 1, 2011, at 5:50 PM, David Winsemius wrote:


I would like to do some operations inside a function using only one value for the last dimension of a table/matrix:

Sorry, I had meant to poste a test dataset:

testdf <- data.frame(x=sample(letters[1:5], 25, replace=TRUE),
                      y=sample(letters[1:5], 25, replace=TRUE),
                      z=sample(letters[1:5], 25, replace=TRUE),
                      event=sample(c(TRUE, FALSE), 25, replace=TRUE) )


tabfn <- function (dfrm, facvec, YN ="event"){
return( Etbl <- do.call(table, dfrm[ , c(facvec, "event") ]) ) # just want Etbl[,,,"TRUE"] or Etbl[,, "TRUE"] or Etbl[,"TRUE"]
             }
tbl <- tabfn(testdf, c("x", "y") )
tbl   # all value of event returned

At the console it is easy for me to count the number of factors and use the right number of commas

tbl[ , , "TRUE"] if I only want the slice with that value. How can I do this programmatically?


I did come up with a solution:

apply(etbl2, 1:(length(dim(Etbl2))-1), "[", 2)

Doesn't seem as elegant as I might have liked but it "works". And I had puzzled and searched in the archives, SO, and multiple books without finding a worked example.


Thnks.

--
David.


David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
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.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
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