turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Need help with For loop

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 24, 2017 2:58 PM
(1131 views)

Hi I have a table like this and what I want to do is:

--> For every row, do a +-1 to X and +-1 to Y and take median of those four Age values and save it under a new column. Can someone please help?

X Y Age

-1 2 27

2 -1 23

4 5 56

-7 -4 43

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 25, 2017 12:52 PM
(2062 views)

Solution

Attached is a sample data table, with the 4 new columns, with their formulas, and the Median column, with it;s formula. Below is an example of the formulas I placed into the data table. This is the X-1, Y column formula

```
dt = Current Data Table();
TheRow = Row();
Xval = :X - 1;
Yval = :Y;
MyRows = dt << get rows where( :X == Xval & :Y == Yval );
If( N Rows( MyRows ) > 0,
TheResult = dt:Age[MyRows[1]]
);
```

Jim

7 REPLIES

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 24, 2017 3:46 PM
(1122 views)

@rk1988 :

Let us assume your table is called "dt"

The following piece of code should add and subtract 1 to X and Y as new columns .

dt << New Column("AddXBias",Numeric,Continuous,Formula(:X+1)) << New Column("SubXBias",Numeric,Continuous,Formula(:X-1)) << New Column("AddYBias",Numeric,Continuous,Formula(:Y+1)) << New Column("SubYBias",Numeric,Continuous,Formula(:Y-1));

Now , the question is which columns you want to take the mean of ?

If it is just the age column , this should do it

AgeMean = Col Mean(:Age);

If it is some other values, please provide clear direction on which values and I will try to help

Best

Uday

Uday

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 24, 2017 4:22 PM
(1116 views)

Thanks for your response :) Let me clarify...

For each row:

1. add 1 to X, now go to that row with coordinates (X+1,Y) and grab the age value --> this is variable Age1

2. subtract 1 from X, now go to that row with coordinates (X-1,Y) and grab the age value --> this is variable Age2

3. add 1 to Y, now go to that row with coordinates (X,Y+1) and grab the age value --> this is variable Age3

4. subtract 1 from Y, now go to that row with coordinates (X,Y-1) and grab the age value --> this is variable Age4

5. Calculate median of Age1...4 and save that under a new column

Repeat steps for each row...

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 24, 2017 4:46 PM
(1109 views)

@rk1988 :

Sorry , not really following your requirement here . If you were to introduce a bias in variables "X" and "Y" as you have explained , the values in column "Age" will not be influenced. Age1 to Age4 and their mean is fixed unless the bias is introduced in Age.

And that mean can be acquired by :

Col Mean(:Age) ;

Apologies that I am unable to follow , would you like to demonstrate what you want in an excel sheet manually , so may be it is more clear .

Best

Uday

Uday

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 24, 2017 5:57 PM
(1092 views)

I am looking for further specification. For a given case where X=3 and Y=-1, are you looking for the value of the column Age, where X=X-1, and Y is unchanged: Using your small sample table

:Age==>where(X==2 and Y==-1)==>23

Is this what you are looking for?

Jim

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 25, 2017 7:05 AM
(1072 views)

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 25, 2017 12:52 PM
(2063 views)

Attached is a sample data table, with the 4 new columns, with their formulas, and the Median column, with it;s formula. Below is an example of the formulas I placed into the data table. This is the X-1, Y column formula

```
dt = Current Data Table();
TheRow = Row();
Xval = :X - 1;
Yval = :Y;
MyRows = dt << get rows where( :X == Xval & :Y == Yval );
If( N Rows( MyRows ) > 0,
TheResult = dt:Age[MyRows[1]]
);
```

Jim

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Oct 25, 2017 1:05 PM
(1039 views)