Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: RE: mata: columns of different eltypes in a transmorphic matrix

 From Nick Cox <[email protected]> To "[email protected]" <[email protected]> Subject Re: st: RE: mata: columns of different eltypes in a transmorphic matrix Date Thu, 10 Oct 2013 13:42:53 +0100

```Naturally, you do often need to combine numeric and string, and the
answer is that strings can include numeric characters, but not vice
versa. So, we can do things like this:

sysuse auto

In Mata:

: make = st_sdata(., "make", "foreign")

: make
1
+------------------+
1 |       Audi 5000  |
2 |        Audi Fox  |
3 |        BMW 320i  |
4 |      Datsun 200  |
5 |      Datsun 210  |
6 |      Datsun 510  |
7 |      Datsun 810  |
9 |    Honda Accord  |
10 |     Honda Civic  |
11 |       Mazda GLC  |
12 |     Peugeot 604  |
13 |  Renault Le Car  |
14 |          Subaru  |
15 |   Toyota Celica  |
16 |  Toyota Corolla  |
17 |   Toyota Corona  |
18 |       VW Dasher  |
19 |       VW Diesel  |
20 |       VW Rabbit  |
21 |     VW Scirocco  |
22 |       Volvo 260  |
+------------------+

: price  = st_data(., "price", "foreign")

: make, strofreal(price, "%2.0f")
1                2
+-----------------------------------+
1 |       Audi 5000             9690  |
2 |        Audi Fox             6295  |
3 |        BMW 320i             9735  |
4 |      Datsun 200             6229  |
5 |      Datsun 210             4589  |
6 |      Datsun 510             5079  |
7 |      Datsun 810             8129  |
8 |     Fiat Strada             4296  |
9 |    Honda Accord             5799  |
10 |     Honda Civic             4499  |
11 |       Mazda GLC             3995  |
12 |     Peugeot 604            12990  |
13 |  Renault Le Car             3895  |
14 |          Subaru             3798  |
15 |   Toyota Celica             5899  |
16 |  Toyota Corolla             3748  |
17 |   Toyota Corona             5719  |
18 |       VW Dasher             7140  |
19 |       VW Diesel             5397  |
20 |       VW Rabbit             4697  |
21 |     VW Scirocco             6850  |
22 |       Volvo 260            11995  |
+-----------------------------------+
Nick
[email protected]

On 10 October 2013 13:27, Joseph Coveney <[email protected]> wrote:
> Nick Cox wrote:
>
> This is an excellent answer, but one simple point seems submerged. An
> elementary strategy is to put numeric variables in a numeric matrix
> and string variables in a string matrix.  I've often done that in Mata
> programs, and never yet felt the need for structs, pointers, and so
> forth.
>
> --------------------------------------------------------------------------------
>
> I agree.   I'm not sure what László's needs are for keeping Stata dataset string
> and numeric data in sync in a single location in Mata.  Perhaps, if he'd like to
> elaborate, someone on the list could offer suggestions that don't entail structs
> or pointer vectors, List or Array classes, and the like.
>
> As you could see from my example, taking the long way round involves additional
> programming steps that in turn give rise to opportunities for logical errors,
> such as -select(X, missing(X[1,]))- instead of -select(X, !missing(X[1,]))-, and
> syntax errors, such as -PointerVector = (A, B)- instead of -PointerVector = &A,
> &B-, among others that I haven't noticed yet.
>
> Joseph Coveney
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```