Consider a list with two items, "table" and "data". "table" is a (n x m) data.frame, "data" is a (r x n) matrix. The matrix has as many columns as there are rows in data.frame. I want to select columns from "table" by a logical expression applied to "data" and get a list with the appropriately subsetted items.

More concrete example:

Let L be the list with item 1 having the columns "A", "B", ... being some factors.

To get a list that is subsetted on A=="some A level value", the "usual way" was:

indx = L$table$A == "some A level value"

L = list( table=L$table[indx,], data=L$data[,indx] )

I would like to get the subset simply by calling L[A=="some A level value"]

For this I will have to overwrite the susbetting method, so the list has to have a class attribute, let's call it "datalist". Then I thought I could define

setClass("datalist", representation="list")

"[.datalist" Error: object 'A' not found

This works:

L[,L$table$A=="a"]

Any ideas?

(btw: I notices that something quite similar is possible for data.tables (library "data.table"), but I could not figure out why it works there but not here...)

Thanks in advance!

More Jochen Wilhelm's questions See All
Similar questions and discussions