Using the cat() function in R [how to concatenate content]

There are times in programming, that you need to combine data. The process known as concatenation combines data for display or saving to a text file. In r this process supplies a way to present material in a more meaningful way, by not only combining it but giving it a degree of formatting.

Description

You use the cat function in r to concatenate the contents of variables. It takes the form of cat(datasets, file, sep, fill, labels, append). Each argument is optional except for the first one. “Datasets” is the list of variables that you intend to concatenate. “File” is the file name to which the results will be saved. If this one is left blank, the results are sent to the console. “Sep” defines how each of the data points is separated. “Fill” controls the pattern by which the output is broken into different lines. “Labels” is a vector defining the labels of the individual lines. “Append” is a Boolean that determines whether to overwrite an existing file or to add results to the end of the file.

Explanation

The cat function works similar to the print function but not exactly. Unlike the unix cat command which combines files, the cat function in r combines distinct types of content into a single string for output. While its input can come from almost any r object, it saves output only to text files and the console. This function can provide a variety of output formats including the addition of line labels.

Examples

Here are several examples of the cat function in action.

> v = c(1:7)
> v
[1] 1 2 3 4 5 6 7
> cat(v, sep=”:”)
1:2:3:4:5:6:7

This example illustrates the standard input and resulting output.

> v = c(1:6)
> v
[1] 1 2 3 4 5 6
> cat(v, sep=”:”, fill = 7, labels = c(“a”,”b”,”c”))
a 1:2:
b 3:4:
c 5:6

This example illustrates the “fill” and “labels” arguments.

> v1 = “This is an example.”
> v2 = “It is only an example.”
> cat(v1,v2)
This is an example. It is only an example.

This example illustrates combining two character strings into one line.

> a = “This is an example.”
> b = “It shows how to use the cat function.”
> c = “The cat function displays data”
> cat(a,b,c,sep=”\n”)
This is an example.
It shows how to use the cat function.
The cat function displays data

This example illustrates combining character strings that are then displayed on three separate lines.

> v3 = c(46, 57,60,63,67)
> v3
[1] 46 57 60 63 67
> cat(v3, file = “demo.txt”, sep=”,”)
Output: 46,57,60,63,67

This example illustrates saving the output to a text file.

Application

One of the applications of the cat function in r is displaying categorical data along with its corresponding numerical data. You could use the categorical data vector as the line labels and the fill argument to make certain that numerical and categorical data line up properly. This is just one of the many applications for the cat function in r, but it is the one that is a natural fit.

The cat function in r is useful for formatting the presentation of data either in the console or stored in text files. It is easy to use while having sufficient options to supply a wide variety of formats. This just may be the data formatting solution that you need.

Scroll to top
Privacy Policy