[R] Access only part of last dimension of table/matrix

Marc Schwartz marc_schwartz at me.com
Sat Jul 2 01:44:48 CEST 2011


On Jul 1, 2011, at 4: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:
> 
> 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?
> 
> Thnks.


David,

I had a vague recollection of something like this coming up at some point in the past and it took me a bit to get the right keywords to find it. 

I did not realize how far back it was (2001), but here are two possible solutions by Peter Dalgaard and Thomas Lumley from the same thread:

https://stat.ethz.ch/pipermail/r-help/2001-October/016110.html
https://stat.ethz.ch/pipermail/r-help/2001-October/016122.html

It looks like Peter's solution is along the lines of the one that you just posted.

Hope that this helps.

Regards,

Marc Schwartz



More information about the R-help mailing list