@@ -7319,60 +7319,76 @@ static gretl_matrix *real_aggregate_by (const double *x,
7319
7319
*/
7320
7320
7321
7321
static void aggr_add_colnames (gretl_matrix * m ,
7322
- const int * ylist ,
7323
7322
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 )
7326
7327
{
7327
7328
char * * S = NULL ;
7329
+ char * * Sl = NULL ;
7330
+ const char * * Sm ;
7328
7331
int i , j , n = m -> cols ;
7332
+ int ny = 0 ;
7333
+ int nx = 0 ;
7329
7334
int err = 0 ;
7330
7335
7331
7336
S = strings_array_new (n );
7332
7337
if (S == NULL ) {
7333
7338
return ;
7334
7339
}
7335
7340
7341
+ ny = ylist != NULL ? ylist [0 ] : Y -> cols ;
7342
+ nx = n - ny - 1 ;
7336
7343
j = 0 ;
7337
7344
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 ] );
7347
7354
}
7348
7355
} 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
+ }
7354
7359
}
7355
7360
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" );
7359
7362
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 ]);
7366
7373
}
7367
7374
} 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 ;
7369
7387
}
7370
7388
}
7371
7389
7372
7390
if (!err ) {
7373
7391
gretl_matrix_set_colnames (m , S );
7374
- } else {
7375
- strings_array_free (S , n );
7376
7392
}
7377
7393
}
7378
7394
@@ -7557,7 +7573,7 @@ gretl_matrix *aggregate_by (const double *x,
7557
7573
}
7558
7574
7559
7575
if (m != NULL ) {
7560
- aggr_add_colnames (m , ylist , xlist , dset , just_count );
7576
+ aggr_add_colnames (m , xlist , ylist , NULL , NULL , dset );
7561
7577
}
7562
7578
7563
7579
free (tmp );
@@ -7747,6 +7763,8 @@ gretl_matrix *matrix_aggregate (const gretl_matrix *X,
7747
7763
}
7748
7764
}
7749
7765
7766
+ aggr_add_colnames (ret , NULL , NULL , X , Y , NULL );
7767
+
7750
7768
gretl_matrix_free (M );
7751
7769
free (counts );
7752
7770
free (cols );
0 commit comments