Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between concatenating strings with cat() and paste()?

What are the differences between concatenating strings with cat and paste?

In particular, I have the following questions.

  1. Why does R not use the double quote (") when it prints the results of calling cat (but it uses quotes when using paste)?

    > cat("test") test > paste("test") [1] "test" 
  2. Why do the functions length and mode, which are functions available for almost all objects in R, not "work" on cat?

    > length(cat("test")) test[1] 0 > mode(cat("test")) test[1] "NULL" 
  3. Why do C-style escape sequences work with cat, but not with paste?

    > cat("1)Line1\n 2)Line2\n 3)Line3") 1)Line1  2)Line2  3)Line3 > paste("1)Line1\n 2)Line2\n 3)Line3") [1] "1)Line1\n 2)Line2\n 3)Line3" 
  4. Why doesn't R's recycling rule work with cat?

    > cat("Grade", c(2, 3, 4, 5)) Grade 2 3 4 5 > paste("Grade", c(2, 3, 4, 5)) [1] "Grade 2" "Grade 3" "Grade 4" "Grade 5" 
like image 335
Sam Avatar asked Oct 08 '12 03:10

Sam


People also ask

What is concatenating a string?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

What is the difference between CAT and print in R?

The simple printing method in R is to use print() . As its name indicates, this method prints its arguments on the R console. However, cat() does the same thing but is valid only for atomic types (logical, integer, real, complex, character) and names, which will be covered in the later chapters.

What is concatenation strings give example?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalisations of concatenation theory, also called string theory, string concatenation is a primitive notion.

Can you concatenate C style strings?

In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.


1 Answers

cat and paste are to be used in very different situations.


paste is not print

When you paste something and don't assign it to anything, it becomes a character variable that is print-ed using print.default, the default method for character, hence the quotes, etc. You can look at the help for print.default for understanding how to modify what the output looks like.

  • print.default will not evaluate escape characters such as \n within a character string.

Look at the answers to this question for how to capture the output from cat.


Quoting from the easy to read help for cat (?cat)

Concatenate and Print

Description

Outputs the objects, concatenating the representations. cat performs much less conversion than print.

...

Details

cat is useful for producing output in user-defined functions. It converts its arguments to character vectors, concatenates them to a single character vector, appends the given sep= string(s) to each element and then outputs them.

Value

None (invisible NULL).

cat will not return anything, it will just output to the console or another connection.

Thus, if you try to run length(cat('x')) or mode(cat('x')), you are running mode(NULL) or length(NULL), which will return NULL.


The help for paste is equally helpful and descriptive

Concatenate Strings

Description

Concatenate vectors after converting to character.

....

Value

A character vector of the concatenated values. This will be of length zero if all the objects are, unless collapse is non-NULL in which case it is a single empty string.

like image 152
mnel Avatar answered Sep 21 '22 04:09

mnel