Concatenating Strings and Numeric Vectors


I need to horizontally concatenate some string vectors  (say , b, c) and some numeric vectors  (say x and y)

In older versions of gauss I could do a~b~x~c~y but I think I cannot do that now. What should I do

1 Answer


The concatenation rules in GAUSS have not changed. Many years ago, before GAUSS supported string arrays, it was common to place string data inside of a regular GAUSS matrix. GAUSS will still allow you to do this. For example:

//create character vector using
//matrix vertical concatenation operator '|'
city = "LA" | "Miami" | "New York" | "Seattle";

The variable city above is a character matrix. That means that it is character data stored inside of a GAUSS matrix. The statement above made a character matrix, because the matrix concatenation operator was used. We can create a string array by using the string vertical concatenation operator, $|. For example:

//create string array using
//string vertical concatenation operator '$|'
city_sa = "LA" $| "Miami" $| "New York" $| "Seattle";

What is the difference between a character vector and a string array in GAUSS?
1. Character data
Since character vectors are text stored inside of a GAUSS matrix, they are easy to mix with numeric data. For example, you can create a mixed matrix by using the matrix concatenation operators, for example:

//create character vector using
//matrix vertical concatenation operator '|'
city = "LA" | "Miami" | "New York" | "Seattle";

population = { 3.8, 0.4, 8.2, 0.6 };

//create 4x2 matrix
city_data = city ~ population;

One thing to keep in mind with character data is that you must you a $ in front of the variable to print the data as character data. For example, if we try to concatenate and print the data above in one compound statement:

print city~population;

we will get this output:

            +DEN       3.8 
            +DEN       0.4 
  3.7879048e+209       8.2 
  9.5338416e-307       0.6

If we want GAUSS to treat the first column as character data when printing, we need to use the $ like this:

print $city;

to receive this output:

        New York 

Finally, since character matrices are stored inside of a GAUSS matrix, each word only has 8 bytes (the size of a double precision floating point value). This means that you cannot place words with more than 8 characters inside of a character vector element.
2. String arrays
GAUSS string arrays support words or phrases of any length. So if you need to use longer variable names, then you should create a string array. You can create a string array by either using the string concatenation operator, or by using the string keyword. For example, either of the statements below will create the same output:

city_sa = { "LA", "Miami", "New York", "Seattle" };
city_sa = "LA" $| "Miami" $| "New York" $| "Seattle";

Since string arrays are created to carry character data, you do not need to use the $ operator to print them.

print city_sa;

will return the following output:

        New York 

However, string arrays cannot be mixed in the same element with a matrix. You may keep the string array and its corresponding data as separate GAUSS variables, or keep them together by making a simple structure in GAUSS.

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy