[R] ReShape to create Time from Observations?

jim holtman jholtman at gmail.com
Wed Jul 8 01:22:34 CEST 2009


Does something like this work for you;  it uses the reshape package:

> 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)
>
> f.melt <-
+ function(df)
+ {
+     # get the starting column number of "Ob1", then extend for rest of columns
+     require(reshape)
+     melt(df, measure=seq(match("Ob1", names(df)), ncol(df)))
+ }
> x.m <- f.melt(X)
> y.m <- f.melt(Y)
> z.m <- f.melt(Z)
>
> # sample data
> head(x.m, 20)
    A B C variable value
1   1 0 1      Ob1     1
2   2 0 1      Ob1     2
3   3 0 1      Ob1     3
4   4 0 1      Ob1     4
5   5 0 1      Ob1     5
6   6 0 1      Ob1     6
7   7 0 1      Ob1     7
8   8 0 1      Ob1     8
9   9 0 1      Ob1     9
10 10 0 1      Ob1    10
11  1 0 1      Ob2     2
12  2 0 1      Ob2     3
13  3 0 1      Ob2     4
14  4 0 1      Ob2     5
15  5 0 1      Ob2     6
16  6 0 1      Ob2     7
17  7 0 1      Ob2     8
18  8 0 1      Ob2     9
19  9 0 1      Ob2    10
20 10 0 1      Ob2    11
>


On Tue, Jul 7, 2009 at 5:37 PM, Mark Knecht<markknecht at gmail.com> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list