Skip to content

Commit e8b75ef

Browse files
committed
aggregate(): add colnames to output in the matrix case
1 parent 40d0d3b commit e8b75ef

File tree

2 files changed

+59
-35
lines changed

2 files changed

+59
-35
lines changed

lib/src/genfuncs.c

+48-30
Original file line numberDiff line numberDiff line change
@@ -7319,60 +7319,76 @@ static gretl_matrix *real_aggregate_by (const double *x,
73197319
*/
73207320

73217321
static void aggr_add_colnames (gretl_matrix *m,
7322-
const int *ylist,
73237322
const int *xlist,
7324-
const DATASET *dset,
7325-
int just_count)
7323+
const int *ylist,
7324+
const gretl_matrix *X,
7325+
const gretl_matrix *Y,
7326+
const DATASET *dset)
73267327
{
73277328
char **S = NULL;
7329+
char **Sl = NULL;
7330+
const char **Sm;
73287331
int i, j, n = m->cols;
7332+
int ny = 0;
7333+
int nx = 0;
73297334
int err = 0;
73307335

73317336
S = strings_array_new(n);
73327337
if (S == NULL) {
73337338
return;
73347339
}
73357340

7341+
ny = ylist != NULL ? ylist[0] : Y->cols;
7342+
nx = n - ny - 1;
73367343
j = 0;
73377344

7338-
if (ylist != NULL && ylist[0] > 0) {
7339-
char **Sy = gretl_list_get_names_array(ylist, dset, &err);
7340-
7341-
if (!err) {
7342-
for (i=0; i<ylist[0]; i++) {
7343-
S[j++] = Sy[i];
7344-
Sy[i] = NULL;
7345-
}
7346-
free(Sy);
7345+
if ((Sl = gretl_list_get_names_array(ylist, dset, NULL)) != NULL) {
7346+
for (i=0; i<ny; i++) {
7347+
S[j++] = Sl[i];
7348+
Sl[i] = NULL;
7349+
}
7350+
free(Sl);
7351+
} else if ((Sm = gretl_matrix_get_colnames(Y)) != NULL) {
7352+
for (i=0; i<ny; i++) {
7353+
S[j++] = gretl_strdup(Sm[i]);
73477354
}
73487355
} else {
7349-
S[j++] = gretl_strdup("byvar");
7350-
}
7351-
7352-
if (!err) {
7353-
S[j++] = gretl_strdup("count");
7356+
for (i=0; i<ny; i++) {
7357+
S[j++] = gretl_strdup("byvar");
7358+
}
73547359
}
73557360

7356-
if (!err && j < n) {
7357-
if (xlist != NULL && xlist[0] > 0) {
7358-
char **Sx = gretl_list_get_names_array(xlist, dset, &err);
7361+
S[j++] = gretl_strdup("count");
73597362

7360-
if (!err) {
7361-
for (i=0; i<xlist[0]; i++) {
7362-
S[j++] = Sx[i];
7363-
Sx[i] = NULL;
7364-
}
7365-
free(Sx);
7363+
if (nx > 0) {
7364+
if ((Sl = gretl_list_get_names_array(xlist, dset, NULL)) != NULL) {
7365+
for (i=0; i<nx; i++) {
7366+
S[j++] = Sl[i];
7367+
Sl[i] = NULL;
7368+
}
7369+
free(Sl);
7370+
} else if ((Sm = gretl_matrix_get_colnames(X)) != NULL) {
7371+
for (i=0; i<nx; i++) {
7372+
S[j++] = gretl_strdup(Sm[i]);
73667373
}
73677374
} else {
7368-
S[j] = gretl_strdup("f(x)");
7375+
for (i=0; i<nx; i++) {
7376+
S[j++] = gretl_strdup("f(x)");
7377+
}
7378+
}
7379+
}
7380+
7381+
/* basic check on validity of @S */
7382+
for (i=0; i<n; i++) {
7383+
if (S[i] == NULL) {
7384+
strings_array_free(S, n);
7385+
err = 1;
7386+
break;
73697387
}
73707388
}
73717389

73727390
if (!err) {
73737391
gretl_matrix_set_colnames(m, S);
7374-
} else {
7375-
strings_array_free(S, n);
73767392
}
73777393
}
73787394

@@ -7557,7 +7573,7 @@ gretl_matrix *aggregate_by (const double *x,
75577573
}
75587574

75597575
if (m != NULL) {
7560-
aggr_add_colnames(m, ylist, xlist, dset, just_count);
7576+
aggr_add_colnames(m, xlist, ylist, NULL, NULL, dset);
75617577
}
75627578

75637579
free(tmp);
@@ -7747,6 +7763,8 @@ gretl_matrix *matrix_aggregate (const gretl_matrix *X,
77477763
}
77487764
}
77497765

7766+
aggr_add_colnames(ret, NULL, NULL, X, Y, NULL);
7767+
77507768
gretl_matrix_free(M);
77517769
free(counts);
77527770
free(cols);

lib/src/gretl_list.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ char *gretl_list_get_names (const int *list, const DATASET *dset,
10801080
* gretl_list_get_names_array:
10811081
* @list: array of integers.
10821082
* @dset: dataset information.
1083-
* @err: location to receive error code.
1083+
* @err: location to receive error code, or #NULL.
10841084
*
10851085
* Returns: An array of strings holding the names of the
10861086
* members of @list, or NULL on failure.
@@ -1093,8 +1093,10 @@ char **gretl_list_get_names_array (const int *list,
10931093
char **S = NULL;
10941094
int i, vi, n;
10951095

1096-
if (list == NULL) {
1097-
*err = E_DATA;
1096+
if (list == NULL || dset == NULL) {
1097+
if (err != NULL) {
1098+
*err = E_DATA;
1099+
}
10981100
return NULL;
10991101
}
11001102

@@ -1106,7 +1108,9 @@ char **gretl_list_get_names_array (const int *list,
11061108

11071109
S = strings_array_new(n);
11081110
if (S == NULL) {
1109-
*err = E_ALLOC;
1111+
if (err != NULL) {
1112+
*err = E_ALLOC;
1113+
}
11101114
return NULL;
11111115
}
11121116

@@ -1118,7 +1122,9 @@ char **gretl_list_get_names_array (const int *list,
11181122
S[i] = gretl_strdup(dset->varname[vi]);
11191123
}
11201124
if (S[i] == NULL) {
1121-
*err = E_ALLOC;
1125+
if (err != NULL) {
1126+
*err = E_ALLOC;
1127+
}
11221128
strings_array_free(S, n);
11231129
S = NULL;
11241130
break;

0 commit comments

Comments
 (0)