[R] Newbie data organisation/structures question...

Gav Wood news at gavofyork.fastmail.fm
Wed Dec 20 17:05:40 CET 2006


Howdo folks,

So my data is in this sort of format:

P  T  I
1  1  (1, 2, 3)
2  1  (2, 4)
1  2  (1, 3, 6, 7)
2  2  (6)

And I want to be able to quickly get:

1: The I when both P and T are given. e.g.:
P = 2, T = 2; I = (6)

2: The concatenated vector of Is when P and a subset of T is given, e.g.:
P = 1, T = 1:2;  Is = (1, 2, 3, 1, 3, 6, 7)

3: The length of that vector.

It would also be nice to have:

4: A list of Is when either P or T is given. e.g.:
P = 2: I = (2, 4), (6)
T = 1: I = (1, 2, 3), (1, 3, 6, 7)

Currently, I have a matrix of P x T, whose elements are lists of a 
single item, the vector I. I call this 'm'.

(1) is easy; just m[P, T][[1]]
(2) and (3) are apparently much harder. For 3, I'm resorting to:

total <- 0
for(p in 1:length(m[,T]))
	total <- total + length(m[p,T][[1]]);

And something similar for 2.

There must surely be a better way of doing this; but what is it?

Cheers,

Gav



More information about the R-help mailing list