m=rand(12, 3:4)
m;
col1 |
col2 |
col3 |
col4 |
3 |
10 |
6 |
5 |
4 |
11 |
6 |
0 |
7 |
2 |
1 |
8 |
a = m.loc(colFilter=[true, true, true, false], view=true)
b = m.loc(colFilter=[true, true, true, false], view=false)
a;
col1 |
col2 |
col3 |
3 |
10 |
6 |
4 |
11 |
6 |
7 |
2 |
1 |
b;
col1 |
col2 |
col3 |
3 |
10 |
6 |
4 |
11 |
6 |
7 |
2 |
1 |
// 若原矩阵发生变化则视图也会改变,副本则不发生变化
m[0,0] = -1
a;
col1 |
col2 |
col3 |
-1 |
10 |
6 |
4 |
11 |
6 |
7 |
2 |
1 |
b;
col1 |
col2 |
col3 |
3 |
10 |
6 |
4 |
11 |
6 |
7 |
2 |
1 |
m = rand(48, 6:8)
m;
col1 |
col2 |
col3 |
col4 |
col5 |
col6 |
col7 |
col8 |
27 |
31 |
47 |
21 |
12 |
43 |
22 |
11 |
3 |
20 |
13 |
37 |
3 |
46 |
27 |
27 |
13 |
5 |
14 |
11 |
26 |
42 |
4 |
18 |
45 |
9 |
31 |
33 |
12 |
19 |
42 |
17 |
2 |
19 |
30 |
25 |
36 |
27 |
21 |
6 |
9 |
36 |
15 |
10 |
29 |
37 |
31 |
42 |
// 通过布尔值过滤
m.loc(rowFilter=[true, true, false, false, true, false])
col1 |
col2 |
col3 |
col4 |
col5 |
col6 |
col7 |
col8 |
27 |
31 |
47 |
21 |
12 |
43 |
22 |
11 |
3 |
20 |
13 |
37 |
3 |
46 |
27 |
27 |
2 |
19 |
30 |
25 |
36 |
27 |
21 |
6 |
m.loc(colFilter=[true, true, false, false, true, false, false, true])
col1 |
col2 |
col3 |
col4 |
27 |
31 |
12 |
11 |
3 |
20 |
3 |
27 |
13 |
5 |
26 |
18 |
45 |
9 |
12 |
17 |
2 |
19 |
36 |
6 |
9 |
36 |
29 |
42 |
// 通过标签过滤
m.rename!(`A`A`B`A`B`B, 2022.01.01 + 0..7)
m;
label |
2022.01.01 |
2022.01.02 |
2022.01.03 |
2022.01.04 |
2022.01.05 |
2022.01.06 |
2022.01.07 |
2022.01.08 |
A |
27 |
31 |
47 |
21 |
12 |
43 |
22 |
11 |
A |
3 |
20 |
13 |
37 |
3 |
46 |
27 |
27 |
B |
13 |
5 |
14 |
11 |
26 |
42 |
4 |
18 |
A |
45 |
9 |
31 |
33 |
12 |
19 |
42 |
17 |
B |
2 |
19 |
30 |
25 |
36 |
27 |
21 |
6 |
B |
9 |
36 |
15 |
10 |
29 |
37 |
31 |
42 |
m.loc(rowFilter=`A);
label |
2022.01.01 |
2022.01.02 |
2022.01.03 |
2022.01.04 |
2022.01.05 |
2022.01.06 |
2022.01.07 |
2022.01.08 |
A |
27 |
31 |
47 |
21 |
12 |
43 |
22 |
11 |
A |
3 |
20 |
13 |
37 |
3 |
46 |
27 |
27 |
A |
45 |
9 |
31 |
33 |
12 |
19 |
42 |
17 |
m.loc(colFilter=2022.01.02);
label |
2022.01.02 |
A |
31 |
A |
20 |
B |
5 |
A |
9 |
B |
19 |
B |
36 |
m.loc(rowFilter=`B, colFilter=2022.01.03)
label |
2022.01.03 |
B |
14 |
B |
30 |
B |
15 |
若 rowFilter / colFilter 是数据对,则 obj 必须是索引矩阵,可通过函数
setIndexedMatrix!
将一个带标签的矩阵设置为索引矩阵。
m = rand(30, 5:6).rename!(1..5, 2022.01.01 + 0..5)
m.setIndexedMatrix!()
m;
label |
2022.01.01 |
2022.01.02 |
2022.01.03 |
2022.01.04 |
2022.01.05 |
2022.01.06 |
1 |
5 |
27 |
26 |
18 |
29 |
3 |
2 |
11 |
12 |
21 |
15 |
3 |
3 |
3 |
1 |
23 |
29 |
17 |
7 |
18 |
4 |
1 |
6 |
12 |
27 |
23 |
23 |
5 |
15 |
7 |
3 |
19 |
4 |
8 |
m.loc(rowFilter=2:4, colFilter=2022.01.03:2022.01.06)
label |
2022.01.03 |
2022.01.04 |
2022.01.05 |
2022.01.06 |
2 |
21 |
15 |
3 |
3 |
3 |
29 |
17 |
7 |
18 |
4 |
12 |
27 |
23 |
23 |
相关函数:at