David Winsemius
2011-Jul-01 21:50 UTC
[R] Access only part of last dimension of table/matrix
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. David Winsemius, MD West Hartford, CT
David Winsemius
2011-Jul-01 23:21 UTC
[R] Access only part of last dimension of table/matrix
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 at 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
Marc Schwartz
2011-Jul-01 23:44 UTC
[R] Access only part of last dimension of table/matrix
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
On 07/02/2011 07:50 AM, 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? >Hi David, I used to do something like this before completely reprogramming the barNest function. # set the first dimension (can be the last also) to a number "stat" sliceargs[[1]] <- x[[stat]] # for the rest of the dimensions of this array, tack on a comma for (arg in 2:ndim) sliceargs[[arg]] <- TRUE sliceargs[[ndim + 1]] <- slice # "slice" the array xslice[[stat]] <- do.call("[", sliceargs) To get the last dimension, just tack commas together for 1:(ndim-1) and then add the number at the end. I think Bill Dunlap gave me this solution when I was struggling with it. Jim
Marc Schwartz
2011-Jul-02 16:04 UTC
[R] Access only part of last dimension of table/matrix
On Jul 2, 2011, at 5:39 AM, peter dalgaard wrote:> > On Jul 2, 2011, at 03:29 , Marc Schwartz wrote: > >> It actually scared me that I had any recollection of an isolated post from 10 years ago. Not sure what to make of that... > > If it is any consolation, the author had forgotten it entirely....LOL Peter! Thanks! Regards, Marc