Replace middle row elements of nested list with new list elements Q kdb - list

Hi so I have created the nested list/matrix:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m
1 2 3
4 5 6
7 8 9
I have also identified the middle column in the list:
q)a:m[0;1],m[1;1],m[2;1]
I now want to replace the middle row (4 5 6) with a to finish with m looking like:
q)m
1 2 3
2 5 8
7 8 9

You've already seen you can index into the matrix with syntax like m[0;1], where 0 refers to the first level of nesting and 1 refers to the second level
KDB also allows you to assign to an index of a list in a similar way e.g.
q)l:1 2 3 4
q)l[1]:20
q)l
1 20 3 4
So you can use something similar in this example:
q)m[1]:a
q)m
1 2 3
2 5 8
7 8 9
As an aside, KDB also allows you to leave out an index, in which case it will take all items from the corresponding level of nesting, e.g.
q)m[0] /first level of nesting i.e. first row
1 2 3
q)m[;0] /second level of nesting i.e. first column
1 4 7
Hope that helps
Jonathon McMurray
AquaQ Analytics

You want to generalise for larger matrices (which must also be square) so your answer needs two parts:
how to construct a
how to insert it
for row/col x where x<count m.
The general expression you want is simply m[x;]:m[;x], because m[x;] denotes row x and m[;x] denotes column x.
See Q for Mortals 3.11.3 Two- and Three-Dimensional Matrices
You can make this a function of the index and the matrix:
q)show m:5 5#1_til 26
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
q){y[x;]:y[;x];:y}[3;m]
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
4 9 14 19 24
21 22 23 24 25

Just adding another approach for you.
q)m:8 cut til 64
q)0 0+\:til 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
q)(m)./:flip 0 0+\:til 8
0 9 18 27 36 45 54 63
q)#[m;4;:;(m)./:flip 0 0+\:til 8]
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q)
For fun, here it is in a function which takes the length&width of the matrix and replaces the 'middle' row with the diagonal values
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}8
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}5
0 1 2 3 4
5 6 7 8 9
0 6 12 18 24
15 16 17 18 19
20 21 22 23 24
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}4
0 1 2 3
4 5 6 7
0 5 10 15
12 13 14 15
q)

q)#[((1 2 3);(4 5 6);(7 8 9));1;:;(2;5;8)]
1 2 3
2 5 8
7 8 9

Indexing in q can be straight forward and I believe a intermediate can be omitted:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m[1]:m[;1]
q)m
1 2 3
2 5 8
7 8 9

Related

VLOOKUL OR OFFSET

I have two sheets in an Excel workbook. I need the formula which creates tables by using vlookup.
I have 10 columns and 10 rows like this
1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
I have tried to use Vlookup with sum but not get the actual results.
The expected result should be like this
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 42 48 56 64 72 80
9 18 27 36 49 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

How to add a row where there is a disruption in series of numbers in Stata

I'm attempting to format a table of 40 different age-race-sex strata to be inputted into R-INLA and noticed that it's important to include all strata (even if they are not present in a county). These should be zeros. However, at this point my table only contains records for strata that are not empty. I can identify places where strata are missing for each county by looking at my strata variable and finding the breaks in the series 1 through 40 (marked with a red x in the image below).
In these places (marked by the red x) I need to add the missing rows and fill in the corresponding county code, strata code, population=0, and the correct corresponding race, sex, age code for the strata.
If I can figure out a way to add an empty row in the spaces with the red Xs from the image, and correctly assign the strata code (and county code) to these empty/missing rows, I am able to populate the rest of the values with the code below:
recode race = 1 & sex= 1 & age =4 if strata = 4
...etc
I'm wondering if there is a way to add the missing rows using an if statement that considers the fact that there are supposed to be forty strata for each county code. It would be ideal if this could populate the correct county code and strata code as well!
Dataex sample data:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float OID str5 fips_statecounty double population byte(race sex age) float strata
1 "" 672 1 1 1 1
2 "" 1048 1 1 2 2
3 "" 883 1 1 3 3
4 "" 1129 1 1 4 4
5 "" 574 1 2 1 5
6 "" 986 1 2 2 6
7 "" 899 1 2 3 7
8 "" 1820 1 2 4 8
9 "" 96 2 1 1 9
10 "" 142 2 1 2 10
11 "" 81 2 1 3 11
12 "" 99 2 1 4 12
13 "" 71 2 2 1 13
14 "" 125 2 2 2 14
15 "" 103 2 2 3 15
16 "" 162 2 2 4 16
17 "" 31 3 1 1 17
18 "" 32 3 1 2 18
19 "" 18 3 1 3 19
20 "" 31 3 1 4 20
21 "" 22 3 2 1 21
22 "" 28 3 2 2 22
23 "" 28 3 2 3 23
24 "" 44 3 2 4 24
25 "" 20 4 1 1 25
26 "" 24 4 1 2 26
27 "" 21 4 1 3 27
28 "" 43 4 1 4 28
29 "" 19 4 2 1 29
30 "" 26 4 2 2 30
31 "" 24 4 2 3 31
32 "" 58 4 2 4 32
33 "" 6 5 1 1 33
34 "" 11 5 1 2 34
35 "" 13 5 1 3 35
36 "" 7 5 1 4 36
37 "" 7 5 2 1 37
38 "" 9 5 2 2 38
39 "" 10 5 2 3 39
40 "" 11 5 2 4 40
41 "01001" 239 1 1 1 1
42 "01001" 464 1 1 2 2
43 "01001" 314 1 1 3 3
44 "01001" 232 1 1 4 4
45 "01001" 284 1 2 1 5
46 "01001" 580 1 2 2 6
47 "01001" 392 1 2 3 7
48 "01001" 440 1 2 4 8
49 "01001" 41 2 1 1 9
50 "01001" 38 2 1 2 10
51 "01001" 23 2 1 3 11
52 "01001" 26 2 1 4 12
53 "01001" 34 2 2 1 13
54 "01001" 52 2 2 2 14
55 "01001" 40 2 2 3 15
56 "01001" 50 2 2 4 16
57 "01001" 4 3 1 1 17
58 "01001" 2 3 1 2 18
59 "01001" 3 3 1 3 19
60 "01001" 6 3 2 1 21
61 "01001" 4 3 2 2 22
62 "01001" 6 3 2 3 23
63 "01001" 4 3 2 4 24
64 "01001" 1 4 1 4 28
65 "01003" 1424 1 1 1 1
66 "01003" 2415 1 1 2 2
67 "01003" 1680 1 1 3 3
68 "01003" 1823 1 1 4 4
69 "01003" 1545 1 2 1 5
70 "01003" 2592 1 2 2 6
71 "01003" 1916 1 2 3 7
72 "01003" 2527 1 2 4 8
73 "01003" 68 2 1 1 9
74 "01003" 82 2 1 2 10
75 "01003" 52 2 1 3 11
76 "01003" 54 2 1 4 12
77 "01003" 72 2 2 1 13
78 "01003" 129 2 2 2 14
79 "01003" 81 2 2 3 15
80 "01003" 106 2 2 4 16
81 "01003" 10 3 1 1 17
82 "01003" 14 3 1 2 18
83 "01003" 8 3 1 3 19
84 "01003" 4 3 1 4 20
85 "01003" 8 3 2 1 21
86 "01003" 14 3 2 2 22
87 "01003" 17 3 2 3 23
88 "01003" 10 3 2 4 24
89 "01003" 4 4 1 1 25
90 "01003" 1 4 1 3 27
91 "01003" 2 4 1 4 28
92 "01003" 2 4 2 1 29
93 "01003" 3 4 2 2 30
94 "01003" 4 4 2 3 31
95 "01003" 10 4 2 4 32
96 "01003" 5 5 1 1 33
97 "01003" 4 5 1 2 34
98 "01003" 3 5 1 3 35
99 "01003" 5 5 1 4 36
100 "01003" 5 5 2 2 38
end
label values race race
label values sex sex
My answer to your previous question
Nested for-loop: error variable already defined
detailed how to create a minimal dataset with all strata present. Therefore you should just merge that with your main dataset and replace missings on the absent strata with whatever your other software expects, zeros it seems.
The complication most obvious at this point is you need to factor in a county variable. I can't see any information on how many counties you have in your dataset, which may affect what is practical. You should be able to break down the preparation into: first, prepare a minimal county dataset with identifiers only; then merge that with a complete strata dataset.

Need help to understand QRegularExpression for my tasks

I have a QTextStream which contain:
Line1: 3 5 7 17 19 23 25
Line2: 3 5 7 17 19 23 26
Line3: 3 5 8 17 19 23 27
Line4: 3 5 9 17 21 35 37
Line5: 3 5 10 17 21 35 38
Line6: 3 5 11 17 21 35 39
Line7: 3 5 12 17 21 36 37
Line8: 3 5 13 17 21 37 38
Line9: 3 5 15 17 21 36 39
Line10: 3 5 16 17 21 37 38
I need to create regular expression to select only lines which contain numbers 3, 17 and 37.
Line4, 7, 8 and 10.
How to setup pattern?
Assuming they are ordered, you might use \b3\b.*\b17\b.*\b37\b.
So in C++, with raw string R"(\b3\b.*\b17\b.*\b37\b)".

Corrupted "cout" library?

OK, so here is what i got going on. I use the latest version of Clion by JetBrains. I run the MinGW GUI as a compiler and use the bundled CMake that comes with the GUI.
So here is my code.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 30; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << " LINE: " << i;
cout << endl;
}
return 0;
}
Very simple and this is what it should output
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
And it continues up until 30.
I know that the actual code is correct because when I run it on a different compiler (On an Ubuntu VM) it comes out perfectly. But when I run it on Clion this is the output:
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
1 2 3 4 5 LINE: 5
1 2 3 4 5 6 LINE: 6
1 2 3 4 5 6 7 LINE: 7
1 2 3 4 5 6 7 8 LINE: 8
1 2 3 4 5 6 7 8 9 LINE: 9
1 2 3 4 5 6 7 8 9 10 LINE: 10
1 2 3 4 5 6 7 8 9 10 11 LINE: 11
1 2 3 4 5 6 7 8 9 10 11 12 LINE: 12
1 2 3 4 5 6 7 8 9 10 11 12 13 LINE: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 LINE: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 LINE: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LINE: 16
1 2 3 4 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LINE: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LINE: 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 LINE: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LINE: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 LINE: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LINE: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 LINE: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 LINE: 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LINE: 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 LINE: 26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 LINE: 27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 181 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 LINE: 28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 LINE: 29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 LINE: 30
Ya, so that clearly doesn't work. And and to solidify the fact that its not my code, when I run it again...
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
1 2 3 4 5 LINE: 5
1 2 3 4 51 2 3 4 5 6 LINE: 6
1 2 3 4 5 6 7 LINE: 7
1 2 3 4 5 6 7 8 LINE: 8
1 2 3 4 5 6 7 8 9 LINE: 9
1 2 3 4 5 6 7 8 9 10 LINE: 10
1 2 3 4 5 6 7 8 9 10 11 LINE: 11
1 2 3 4 5 6 7 8 9 10 11 12 LINE: 12
1 2 3 4 5 6 7 8 9 10 11 12 13 LINE: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 LINE: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 LINE: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LINE: 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LINE: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LINE: 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 LINE: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LINE: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 LINE: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LINE: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 LINE: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 LINE: 24
1 2 3 4 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LINE: 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 LINE: 26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 LINE: 27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 LINE: 28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 LINE: 29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 LINE: 30
And if ran it again it would once again be different. It never the same and is always doing the same thing more or less.
This issue only seems to exist when I run programs that spit out long cout's. For example the HelloWorld runs fine. And as the last kick in the nuts, I ran it on my other computer (same setup with Clion and MinGW) and the exact same thing was happening.

SQL Update Subsequent Column OFFSET FETCH NEXT

I like to know is there a way to doing auto looping / counter batch, updating SQL column like using OFFSET / FETCH NEXT method
QUESTION : Below table have 20 rows, I like to update DealerId column the First 4 rows as 1,2,3,4 and the next subsequent 4 rows repeating as 1,2,3,4 values
Something like below
NEED TO MODIFY TABLE
Id DealerId
1 1 1
2 2 2
3 3 3
4 4 4
5 5 1
6 6 2
7 7 3
8 8 4
9 9 1
10 10 2
11 11 3
12 12 4
13 13 1
14 14 2
15 15 3
16 16 4
17 17 1
18 18 2
19 19 3
20 20 4
ORIGINAL TABLE
Id DealerId StoreId TerminalId MessageNo CreatedDate
1 1 86 5027 029500021201403031434350039 2014-03-03 14:34:37.347
2 2 86 5027 029500021201403031434350039 2014-03-05 10:31:59.903
3 3 86 5027 029500021201403031434350039 2014-03-05 10:33:41.293
4 4 86 5027 029500021201403031434350039 2014-03-05 10:46:50.057
5 5 86 5027 029500021201403031434350039 2014-03-05 10:50:23.910
6 6 33 5338 004000003201403051508010255 2014-03-05 15:08:03.247
7 7 26 5595 704201181201403061024330013 2014-03-06 10:24:34.590
8 8 26 5595 704201181201403061026180022 2014-03-06 10:26:19.517
9 9 33 5338 004000003201403061043150312 2014-03-06 10:43:16.013
10 10 86 5027 029500021201403031434350039 2014-03-06 14:27:51.717
11 11 86 5027 029500021201403031434350039 2014-03-06 14:38:40.593
12 12 86 5027 029500021201403031434350039 2014-03-06 14:44:25.947
13 13 521 4905 051100003002447 2014-03-07 12:51:07.487
14 14 521 4905 051100003002447 2014-03-07 12:55:07.300
15 15 521 4905 051100003002447 2014-03-07 12:56:24.793
16 16 521 4905 051100003002447 2014-03-07 12:57:43.123
17 17 521 4905 051100003002447 2014-03-07 14:15:11.093
18 18 632 5120 088800003201403071441280026 2014-03-07 14:41:29.733
19 19 632 5120 088800003201403071456500050 2014-03-07 14:56:51.727
20 20 632 5120 088800003201403071459240064 2014-03-07 14:59:24.953
Assuming that all id's are consequently, starting from 1:
In MySQL:
update OriginalTable
set DealreId = mod(id-1, 4)+1
and in Microsoft SQL Server:
update OriginalTable
set DealreId = ((id-1)%4)+1
And if the id's are not consequently (or are not starting from 1) you can use cursor to update it one by one:
DECLARE c1 CURSOR FOR
SELECT id, dealerId
FROM OriginalTable
ORDER BY id, dealerId
OPEN c1
declare #id int
declare #dealerId int
declare #i int
set #i = 1
FETCH NEXT FROM c1
INTO #id, #dealerId
while ##FETCH_STATUS = 0
BEGIN
update OriginalTable
set dealerId = #i
where current of c1
if (#i < 4)
set #i = #i + 1
else
set #i = 1
FETCH NEXT FROM c1
INTO #id, #dealerId
END