Contributor

Joined:

May 28, 2018

## Creating Time Variable

Okay. So when looking at the selected area only. I would want:

Nback#=1, duration=14303912, Cumulative Duration=0

Nback#=2, duration=14305914, Cumulative Duration=2002

Nback#=3, duration=14307916, Cumulative Duration=4004

Nback#=4, duration=14309965, Cumulative Duration=6053

Nback#=5, duration=14312015, Cumulative Duration=8103

Nback#=6, duration=14314051, Cumulative Duration=10139

I need to make this final column as a measure of time. Any guidance would be greatly appreciated.

7 REPLIES

Occasional Contributor

Joined:

Jun 4, 2018

## Re: Creating Time Variable

This script might be what you need. Data set as the attchment.

``````

dt = Current Data Table();
//dt << New Column( "Delta", numeric );
dt << New Column( "Cumulative", numeric );
dt << Sort( By( :Category, :Time ), Order( Ascending, Ascending ), Replace table );

For( i = 1, i <= N Rows( dt ), i++,
If( :Time[i] == Min( :Time[dt << get rows where( :Category == :Category[i] )] ),
//dt:Delta[i] = 0;
dt:Cumulative[i] = 0;
,
//dt:Delta[i] = dt:Time[i] - dt:Time[i - 1];
dt:Cumulative[i] = dt:Cumulative[i-1]+ (dt:Time[i] - dt:Time[i - 1]);
)
);

``````
Highlighted

Community Trekker

Joined:

Sep 30, 2016

## Re: Creating Time Variable

Here is a way to do this with just column formulas.  I'm not sure which column you are using to indicate the time reset, so I included

1. A method to do this from either a column whose values changes for each new group, and
2. A method based on a column that indicates a reset in time.
``````Names default to here ( 1 );

//Table with data similar to yours
dt = New Table( "Data",
New Column( "Space", Numeric, "Continuous",
Format( "Best", 12 ),
Formula( Round( Random Uniform( 1, 5 ), 0 ) )
),
New Column( "Duration", Numeric, "Continuous",
Format( "Best", 12 ),
Formula( Round( Random Uniform( 1998, 2002 ), 0 ) )
),
New Column( "Start", Numeric, "Continuous",
Format( "Best", 12 ),
Formula( If( Row() == 1, :Space, :Space + Lag( :Start, 1 ) + Lag( :Duration, 1 ) ) )
),
New Column( "End", Numeric, "Continuous",
Format( "Best", 12 ),
Formula( :Start + :Duration )
),
New Column( "Period Indication", Character, "Nominal",
Formula( If( Row() < 15, "One", "Two" ) )
),
New Column( "Indication of Time Reset Pulse", Numeric, "Nominal",
Formula( If( Row() == 10, 1, . ) )
)
);

//New Column - subtract the current end time from the first end time in
//the group.  Use this if the column :summary_image_end" tells you when
//to start a new group
dt << New Column( "Time In Group", Numeric, "Continuous",
Format( "Best", 12 ),
Formula( :End - Col Min( :Start, :Period Indication ) )
);

//Make a New Column indicating the period for a pulse signal, use this if
//the column Lag1 tells you when to start a new group
dt << New Column( "Period Indication from Pulse", Numeric, "Nominal",
Format( "Best", 12 ),
Formula(
If(
Row() == 1,
1,
Lag( :Period Indication from Pulse, 1 ) +
If(
Is Missing( :Indication of Time Reset Pulse ),
0,
1
)
)
)
);

//Once the group column is made then calculate the duration
dt << New Column( "Time In Group - Pulse", Numeric, "Continuous",
Format( "Best", 12 ),
Formula( :End - Col Min( :Start, :Period Indication from Pulse ) )
);``````

Contributor

Joined:

May 28, 2018

## Re: Creating Time Variable

So upon looking at the data that I have I realized that I have a 'Nback Duration' Column. I was wondering if there would be an easier way to get to the desired end result using this column and the lag1 column.

Community Trekker

Joined:

Sep 30, 2016

## Re: Creating Time Variable

You could sum up the durations however you might miss the time between rows. In my opinion using the Start and End Times (Nback Start and Nback Stop and summary_image_end) is fairly succinct.  The formula should just have to be as shown below.  You would use *either* the blue columns (if you care about summary_image_end) or the red columns (if you care about Lag1).

Contributor

Joined:

May 28, 2018

## Re: Creating Time Variable

So, this works technically. It does give me values, but it does not rest at the beginning of each trial. This is an important step in this process so that I am able to compare across each individual trial. The current script allows me to compare overtime generally, not within each of the individual trials. This is why I would like to have each new trial start at 0 time.

Community Trekker

Joined:

Sep 30, 2016

## Re: Creating Time Variable

Maybe you want to start counting from the end time of the first row in each period?

Contributor

Joined:

May 28, 2018

## Re: Creating Time Variable

So, making this small change still produces the same sort of output for my new variable.