[R] Access only part of last dimension of table/matrix
David Winsemius
dwinsemius at comcast.net
Sat Jul 2 03:15:04 CEST 2011
On Jul 1, 2011, at 7:44 PM, Marc Schwartz wrote:
> 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.
Yeah. Thanks, Mark. Pretty much the same. Guess I'm in good company.
(Surprised this isn't asked more frequently.)
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) )
etbl <- table(testdf[ , c(c("x", "y"), "event")])
apply(etbl, seq(length=length(dim(etbl))-1),"[", 2) # Dalgaard 2001
apply(etbl, 1:(length(dim(etbl))-1), "[", 2) # Winsemius 2011
Any idea how to make it a "selection"? By that I mean how to select
values of the last dimension whose "event" values are "TRUE". I get
"Error: object 'event' not found" with any expression involving event,
since it is only an attribute in dimnames.
>
> Hope that this helps.
Yes. At least there is nothing (yet) that I would call "truly
elegant". Maybe it's just that I stumbled on my solution, rather than
seeing it as a glaringly obvious application of apply(..., "[" , index)
>
> Regards,
Sorry for the duplicate private message. Meant to hit reply to all.
>
> Marc Schwartz
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list