cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMPĀ® Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-241410%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3Ecalculating%20sum%20of%20lag%20x%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-241410%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI%20am%20trying%20to%20create%20a%20column%20that%20calculates%20the%20sum%20of%20the%20lag%2024%20rows.%20In%20this%20instance%20I%20have%20a%20column%20%22Precip%22%20that%20contains%20hourly%20precipitation%20totals.%20There%20is%20a%20second%20column%26nbsp%3B%20%22precipitation%20station%22%20denoting%20from%20which%20location%20each%20value%20was%20recorded.%20I%20have%20sorted%20the%20columns%20based%20on%20%22DateTime%22%20and%20the%20%22Precipitation%20Station%22.%20I%20am%20looking%20for%20an%20efficient%20way%20to%20sum%20the%20previous%2024%20values%20in%20the%20Precip%20column%20thus%20creating%20a%20new%20column%20that%20tells%20me%20how%20much%20it%20rained%20in%20the%20previous%2024%20hours.%20This%2024%20hour%20total%20is%20station%20specific%20so%20it%20cannot%20calculate%20the%20lag%2024%20values%20if%20the%20lag%2024%20precipitations%20do%20not%20match.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-241590%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20calculating%20sum%20of%20lag%20x%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-241590%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI%20ended%20up%20figuring%20this%20out%20on%20my%20own%2C%20it%20is%20a%20simple%20summation%20function.%20Figured%20i'd%20post%20it%20in%20case%20anyone%20else%20needs%20to%20do%20something%20like%20this.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EIf(%20%3APrecipitation%20Station%20%3D%3D%20Lag(%20%3APrecipitation%20Station%2C%201%20)%2C%0A%20Summation(%20i%20%3D%201%2C%2024%2C%20Lag(%20%3APrecipitation%2C%20i%20)%20)%2C%0A%20%3APrecipitation%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-241437%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20calculating%20sum%20of%20lag%20x%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-241437%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI'm%20not%20actually%20trying%20to%20compute%20the%20total%20precip%20for%20a%20given%20station%2Fdate%2C%20but%20rather%20station%2Fprevious%2024%20hour%20period.%20so%20for%20example%20if%20the%20DateTime%20for%20a%20given%20for%20is%201%2F3%2F2020%2009%3A00%3A00%20AM%20I%20would%20want%20to%20take%20the%20previous%2024%20values%20in%20the%20precip%20column%20and%20sum%20those.%20So%20starting%20with%201%2F3%2F2020%2008%3A00%3A00AM%20and%20going%20all%20the%20way%20back%20to%201%2F2%2F2020%2009%3A00%3A00%20AM.%20Essentially%20a%20moving%2024%20hour%20window%20of%20time.%20This%20column%20would%20thus%20give%20me%20the%20previous%20rain%20over%20the%20prior%2024%20hours%20for%20each%20row%20in%20my%20data%20table.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-241436%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20calculating%20sum%20of%20lag%20x%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-241436%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Precip.png%22%20style%3D%22width%3A%20734px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Precip.png%22%20style%3D%22width%3A%20734px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Precip.png%22%20style%3D%22width%3A%20734px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F21000iD4CFF40B4B8FBF7D%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Precip.png%22%20alt%3D%22Precip.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-241434%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20calculating%20sum%20of%20lag%20x%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-241434%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EIf%20you%20are%20trying%20to%20calculate%20the%20sum%20of%20precipitation%20for%20a%20given%20day%2Fstation%2C%20create%20a%20new%20column%20which%20is%20just%20the%20date%20part%20of%20the%20datetime.%26nbsp%3B%20Then%20do%20a%20tabulation%20by%20date%20and%20station%2C%20and%20put%20Precip%20in%20the%20middle.%3C%2FP%3E%0A%3CP%3EThis%20program%20creates%20an%20example%20dataset%20with%20a%20formula%20column%20for%20date%20and%20a%20Tabulate%20program%20to%20calculate%20the%20precipitation%20by%20date%2Fstation.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENew%20Table(%20%22Example%22%2C%20Add%20Rows(%2020%20)%2C%0A%20New%20Script(%20%22Tabulate%22%2C%20%0A%20%20Tabulate(%0A%20%20%20Show%20Control%20Panel(%200%20)%2C%0A%20%20%20Add%20Table(%0A%20%20%20%20Column%20Table(%20Analysis%20Columns(%20%3APrecip%20)%20)%2C%0A%20%20%20%20Row%20Table(%20Grouping%20Columns(%20%3APrecip%20Date%2C%20%3AStation%20)%20)%0A%20%20%20)%0A%20%20)%0A%20)%2C%0A%20New%20Column(%20%22Precip%20Datetime%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%20%22ddMonyyyy%20h%3Am%3As%22%2C%2022%2C%200%20)%2C%0A%20%20Input%20Format(%20%22ddMonyyyy%20h%3Am%3As%22%2C%200%20)%2C%0A%20%20Set%20Values(%0A%20%20%20%5B3660710400%2C%203660714000%2C%203660717600%2C%203660721200%2C%203660724800%2C%203660710400%2C%0A%20%20%203660714000%2C%203660717600%2C%203660721200%2C%203660724800%2C%203660796800%2C%203660800400%2C%0A%20%20%203660804000%2C%203660807600%2C%203660811200%2C%203660796800%2C%203660800400%2C%203660804000%2C%0A%20%20%203660807600%2C%203660811200%5D%0A%20%20)%2C%0A%20%20Set%20Display%20Width(%20186%20)%0A%20)%2C%0A%20New%20Column(%20%22Station%22%2C%20Character%2C%20%22Nominal%22%2C%0A%20%20Set%20Values(%0A%20%20%20%7B%22A%22%2C%20%22A%22%2C%20%22A%22%2C%20%22A%22%2C%20%22A%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%2C%20%22A%22%2C%20%22A%22%2C%20%22A%22%2C%20%22A%22%2C%0A%20%20%20%22A%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%7D%0A%20%20)%0A%20)%2C%0A%20New%20Column(%20%22Precip%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%20%22Best%22%2C%2012%20)%2C%0A%20%20Set%20Values(%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%203%2C%204%2C%205%2C%201%2C%201%2C%204%2C%205%2C%206%2C%207%2C%205%2C%204%2C%205%2C%206%2C%207%2C%208%5D%20)%0A%20)%2C%0A%20New%20Column(%20%22Precip%20Date%22%2C%20Character%2C%20%22Nominal%22%2C%0A%20%20Formula(%20Short%20Date(%20%3APrecip%20Datetime%20)%20)%0A%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
RVhydrA
Level III

calculating sum of lag x rows

I am trying to create a column that calculates the sum of the lag 24 rows. In this instance I have a column "Precip" that contains hourly precipitation totals. There is a second column  "precipitation station" denoting from which location each value was recorded. I have sorted the columns based on "DateTime" and the "Precipitation Station". I am looking for an efficient way to sum the previous 24 values in the Precip column thus creating a new column that tells me how much it rained in the previous 24 hours. This 24 hour total is station specific so it cannot calculate the lag 24 values if the lag 24 precipitations do not match. 

1 ACCEPTED SOLUTION

Accepted Solutions
RVhydrA
Level III

Re: calculating sum of lag x rows

I ended up figuring this out on my own, it is a simple summation function. Figured i'd post it in case anyone else needs to do something like this. 

 

 

If( :Precipitation Station == Lag( :Precipitation Station, 1 ),
	Summation( i = 1, 24, Lag( :Precipitation, i ) ),
	:Precipitation
);

 

 

View solution in original post

4 REPLIES 4
pmroz
Super User

Re: calculating sum of lag x rows

If you are trying to calculate the sum of precipitation for a given day/station, create a new column which is just the date part of the datetime.  Then do a tabulation by date and station, and put Precip in the middle.

This program creates an example dataset with a formula column for date and a Tabulate program to calculate the precipitation by date/station.

New Table( "Example", Add Rows( 20 ),
	New Script( "Tabulate", 
		Tabulate(
			Show Control Panel( 0 ),
			Add Table(
				Column Table( Analysis Columns( :Precip ) ),
				Row Table( Grouping Columns( :Precip Date, :Station ) )
			)
		)
	),
	New Column( "Precip Datetime", Numeric, "Continuous", Format( "ddMonyyyy h:m:s", 22, 0 ),
		Input Format( "ddMonyyyy h:m:s", 0 ),
		Set Values(
			[3660710400, 3660714000, 3660717600, 3660721200, 3660724800, 3660710400,
			3660714000, 3660717600, 3660721200, 3660724800, 3660796800, 3660800400,
			3660804000, 3660807600, 3660811200, 3660796800, 3660800400, 3660804000,
			3660807600, 3660811200]
		),
		Set Display Width( 186 )
	),
	New Column( "Station", Character, "Nominal",
		Set Values(
			{"A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "A", "A", "A", "A",
			"A", "B", "B", "B", "B", "B"}
		)
	),
	New Column( "Precip", Numeric, "Continuous", Format( "Best", 12 ),
		Set Values( [1, 2, 3, 4, 5, 3, 4, 5, 1, 1, 4, 5, 6, 7, 5, 4, 5, 6, 7, 8] )
	),
	New Column( "Precip Date", Character, "Nominal",
		Formula( Short Date( :Precip Datetime ) )
	)
);
pmroz
Super User

Re: calculating sum of lag x rows

Precip.png

RVhydrA
Level III

Re: calculating sum of lag x rows

I'm not actually trying to compute the total precip for a given station/date, but rather station/previous 24 hour period. so for example if the DateTime for a given for is 1/3/2020 09:00:00 AM I would want to take the previous 24 values in the precip column and sum those. So starting with 1/3/2020 08:00:00AM and going all the way back to 1/2/2020 09:00:00 AM. Essentially a moving 24 hour window of time. This column would thus give me the previous rain over the prior 24 hours for each row in my data table.

RVhydrA
Level III

Re: calculating sum of lag x rows

I ended up figuring this out on my own, it is a simple summation function. Figured i'd post it in case anyone else needs to do something like this. 

 

 

If( :Precipitation Station == Lag( :Precipitation Station, 1 ),
	Summation( i = 1, 24, Lag( :Precipitation, i ) ),
	:Precipitation
);