[R] ReShape to create Time from Observations?

Mark Knecht markknecht at gmail.com
Wed Jul 8 15:02:04 CEST 2009


On Tue, Jul 7, 2009 at 4:22 PM, jim holtman<jholtman at gmail.com> wrote:
> 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
>>
<SNIP>

Jim,
   It wasn't exactly what I was looking for but I think the ideas plus
a bit of off-list help from another member helped me get much closer.
The idea of using match is very helpful in my case because I'm able to
leverage the fact that in my data files everything to the right is
also an observation to easily create  list to the end of the row. Try
the following:

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)

BrkPnt<-match("Ob1",names(X))
Ob_Group <- list(names(X)[BrkPnt:ncol(X)])

# Give to reshape to turn ObX into time
answerX1<- reshape(X, varying=Ob_Group, direction='long')

and at this point I can subset based on id or some other variable:

subset(answerX1, A==1)
    A B C time Ob1 id
1.1 1 0 1    1   1  1
1.2 1 0 1    2   2  1
1.3 1 0 1    3   3  1
1.4 1 0 1    4   4  1
1.5 1 0 1    5   3  1
1.6 1 0 1    6   2  1

   I *think* this is data that I can sent to matplot/qplot and get
charts that I'm interested in. I'll work on that today to verify but
it looks about right to me using this simple case:

PlotData<-subset(answerX1, A==1)
matplot(PlotData$time,PlotData$Ob1)

   I really like the match idea. The first observation should
generally be in about the first 20 columns of my files which can
potentially be thousands of columns wide. There's no reason in my case
to match every other column to the right as I already know they will
match. I can get a list of all the observations with BrkPnt:ncol(X) or
all the independent variables using 1:BrkPnt-1. I could also, if I
chose, extract a specific group of observations by matching Ob20 and
Ob40 to potentially find observations taken in a certain time period
every day, etc. Nice!

   I'll put it back in a function as you did for use in my actual code.

Cheers,
Mark

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