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

Michael Kubovy kubovy at virginia.edu
Wed Dec 20 19:14:48 CET 2006


On Dec 20, 2006, at 11:05 AM, Gav Wood wrote:

> 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)

Not knowing why you organized the data as you did, let me suggest  
another approach:

iv <- c(1, 2, 3, 2, 4, 1, 3, 6, 7, 6)
p <- c(1, 1, 1, 2, 2, 1, 1, 1, 1, 2)
t <- rep(1:2, each = 5)
dat <- data.frame(iv, p, t)

> And I want to be able to quickly get:
>
> The I when both P and T are given. e.g.:
> P = 2, T = 2; I = (6)

subset(dat, p == 2 & t ==2)$iv

> 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)

(iv1 <- subset(dat, p == 1)$iv)

> The length of that vector.

length(iv1)

> 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)

list(p2t1 = subset(dat, p == 2 & t ==1)$iv, p2t2 = subset(dat, p == 2  
& t ==2)$iv)
list(p1t1 = subset(dat, p == 1 & t ==1)$iv, p1t2 = subset(dat, p == 1  
& t ==2)$iv) # correcting your requirement to get your result

There are many other ways of getting the results you need as Marc  
Schwartz pointed out in his reply.
_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/



More information about the R-help mailing list