Spotfire data table merge using nearest neighbour values - merge

I am trying to merge two data tables (tables A and B) in Spotfire 7.10 using insert columns to give the resultant table C. My problem is i cannot get the join i need on Depth because Depth in tables A and B are not exact matches. What i need is to match Table B to Table A based on a match using Depth to its nearest value i.e Depth 10.5 (table B) matches Depth 10 (Table A). Is this possible in Spotfire or using an TERR R script?
Table A
Depth data
10 2
20 4
30 3
40 5
50 7
Table B
Depth data 2
10.5 100
30.5 112
50.5 125
Table C
Depth data data 2
10 2 100
20 4
30 3 112
40 5
50 7 125
many thanks for any help

It depends on the range of values you may have in both tables for Depth, but you may find that simply rounding the result to the nearest 10 in Table B will suffice. Then you can join based on this.
Round([Depth]/10,0)*10

Related

Running total using two columns

Given a table with data like:
A
B
Qty.
Running Total
5
5
5
10
5
15
I can create the running total using the formula =SUM($A$2:A2) and then drag down to get the running total after each quantity (here Qty.)
What may I do for calculating running total using two columns which may or may not be consecutive as shown below:
A
B
C
D
Qty. 1
Other
Qty. 2
RT
2
blah
2
4
2
phew
2
8
3
xyz
2
13
Place in cell D2 the formula =SUM(A2,C2,D1). Do not pay attention to the fact that the function will refer to a non-numeric cell D1 - the SUM() function will not break, unlike ordinary addition =A2+C2+D1. Now, just stretch the formula down.

Calculating group means with own group excluded in MATLAB

To be generic the issue is: I need to create group means that exclude own group observations before calculating the mean.
As an example: let's say I have firms, products and product characteristics. Each firm (f=1,...,F) produces several products (i=1,...,I). I would like to create a group mean for a certain characteristic of the product i of firm f, using all products of all firms, excluding firm f product observations.
So I could have a dataset like this:
firm prod width
1 1 30
1 2 10
1 3 20
2 1 25
2 2 15
2 4 40
3 2 10
3 4 35
To reproduce the table:
firm=[1,1,1,2,2,2,3,3]
prod=[1,2,3,1,2,4,2,4]
hp=[30,10,20,25,15,40,10,35]
x=[firm' prod' hp']
Then I want to estimate a mean which will use values of all products of all other firms, that is excluding all firm 1 products. In this case, my grouping is at the firm level. (This mean is to be used as an instrumental variable for the width of all products in firm 1.)
So, the mean that I should find is: (25+15+40+10+35)/5=25
Then repeat the process for other firms.
firm prod width mean_desired
1 1 30 25
1 2 10 25
1 3 20 25
2 1 25
2 2 15
2 4 40
3 2 10
3 4 35
I guess my biggest difficulty is to exclude the own firm values.
This question is related to this page here: Calculating group mean/medians in MATLAB where group ID is in a separate column. But here, we do not exclude the own group.
p.s.: just out of curiosity if anyone works in economics, I am actually trying to construct Hausman or BLP instruments.
Here's a way that avoids loops, but may be memory-expensive. Let x denote your three-column data matrix.
m = bsxfun(#ne, x(:,1).', unique(x(:,1))); % or m = ~sparse(x(:,1), 1:size(x,1), true);
result = m*x(:,3);
result = result./sum(m,2);
This creates a zero-one matrix m such that each row of m multiplied by the width column of x (second line of code) gives the sum of other groups. m is built by comparing each entry in the firm column of x with the unique values of that column (first line). Then, dividing by the respective count of other groups (third line) gives the desired result.
If you need the results repeated as per the original firm column, use result(x(:,1))

How can I calculate the average between two text files based on matrixes?

I am fairly new to using MATLAB. I have two text files that have 500 rows x 100 columns and every one of it has a random value.
For example:
1 2 3 4 5
6 7 8 9 2
4 5 6 6 8
So my question is, corresponding to the row and column, how can I calculate the average of each particular field corresponding to the row and column?
For instance, at (1,1) in text1.txt is 20 and at (1,1) in text2.txt is 40. The average is 30 and I want to display this result in an another file at (1,1).
Many thanks.
assuming the files are named 'a' and 'b':
load a
load b
c=(a+b)/2;
save('c','c','-ascii');

select every 3rd participate from a list and make and average in matlab

I got 3 lists with grades ranging from 0-100 represting 3 different tests.
each list has an equal number of indxes (represting participates).
For example- the 1st indexes in the lists- list1,list2 and list3, are the grades of the first particiapte in the 3 different tests.
I need to make a new group (named group1) that select evey 3rd participate, starting from the first, and than calculate the avarage of this group scores.
i'll appriciate any help!!
Hopefully instead of three 'lists' you are actually using a 3 column matrix for this? e.g.
testScores = [20 48 13;
85 90 93;
54 50 56;
76 80 45
...]
From here it is trivial to select every third participant:
testScores(1:3:end, :)
and then to find the mean:
mean(testScores(1:3:end,:),2)

How to Sum values of column corresponding to one column in jasper

In jasper i have one requirement like, sum all the data of the column based on another column. I know how to show sum of all the data in the column. But please suggest me for this requirement.
Emp-Category Emp-Id Salary
---------------------------------------
Cate - A 1 128
2 50
3 89
total 267
Cate - B 4 123
5 50
6 100
total 273
Total Expenses 540
So there will be many number of categories, which will be getting from database.
Please suggest me how to do this approach.
In that case, you can use groups...
You can refer to the link below for tutorial :)
http://www.tutorialspoint.com/jasper_reports/jasper_report_groups.htm