[R] ReShape to create Time from Observations?

Mark Knecht markknecht at gmail.com
Tue Jul 7 23:37:31 CEST 2009


Here is a couple of very simple data.frames:

X<-data.frame(A=1:10, B=0, C=1, Ob1=1:10, Ob2=2:11, Ob3=3:12,
Ob4=4:13, Ob5=3:12, Ob6=2:11)
Y<-data.frame(A=1:20, B=0, C=1, D=5, Ob1=1:10, Ob2=2:11, Ob3=3:12,
Ob4=4:13, Ob5=3:12, Ob6=2:11, Ob7=5:9)
Z<-data.frame(A=1:30, B=0, C=1, D=6, E=1:2, Ob1=1:10, Ob2=2:11,
Ob3=3:12, Ob4=4:13, Ob5=3:12, Ob6=2:11, Ob7=1:10, Ob8=3:12)

Each row in the data.frame is a unique experiment. The fields Ob1:Ob6
(in the case of the first data.frame) represent observations taken at
fixed intervals for specific that experiment. (Observation 1,
Observation2, etc.) IMPORTANT - Different data files have different
numbers of both experiments and observations as well as different
observation rates. Some data.frames might have 50
observations/experiment at 10 minute intervals (a work day) while
others might have 2000 observations/experiment at daily intervals
representing years of data. The number of columns preceding OB1 varies
from file to file but once I get to Ob1 I have set it up so that the
names to the right are consecutive to the end of the row, so 2000
observations will have names Ob1:Ob2000.

How could I use ReShape to create a generic new data.frame where all
of the ObX columns become 'time' for the experiments in that
data.frame? I.e. - Ob1:ObX become s single variable called time
incrementing from 1:X.

The generic answer cannot use any numbers like 1:3 or 4:12 because
every file is different. I think I need to discover the dimensions of
the data.frames and locations of Ob1 as well as the name of the last
column, etc., to construct the right fields. We could (if it's legal
in R) say things like Ob1:Ob11 but it doesn't seem legal. I do see I
can say things like names(X[4]) to discover Ob1, and cute things like
names(X[dim(X)[2]]) to get the last name, etc., but I cannot put it
together how to use this to drive ReShape into making all the
Observations into a single variable called time.

I hope this is clear.

Thanks,
Mark




More information about the R-help mailing list