Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does R have a distinction between character vectors and strings?

As far as I know, what most languages call a string, R calls a character vector. For example, "Alice" is not a string, it's a character vector of length 1. Similarly, c("Alice", "Bob") is a character vector of length 2. I cannot recall my IDE or any of my work with R's type system telling me that R has any internal concept of "strings".

Despite this, R's documentation frequently uses the word "string":

  • ?paste and ?nchar frequently talk of "character strings".
  • Many "See Also" sections mention strings without any qualifier, e.g. ?paste, ?chartr, and ?agrep.
  • ?strsplit mentions "substrings".
  • ?agrep, ?toString, and ?adist talk about strings both in their titles and "Description" sections.
  • strsplit, strwidth, and toString have string or a shorthand for it in their names.

So does R actually have a concept of strings, or does it always mean exactly the same thing as "character vector"?

like image 522
J. Mini Avatar asked Oct 22 '25 08:10

J. Mini


1 Answers

Converting my comment to an answer.

A description of character and string can be found in the R Language Definition:

R has six basic (‘atomic’) vector types: logical, integer, real, complex, string (or character) and raw. The modes and storage modes for the different vector types are listed in the following table.

typeof mode storage.mode
logical logical logical
integer numeric integer
double numeric double
complex complex complex
character character character
raw raw raw

[...]

String vectors have mode and storage mode "character". A single element of a character vector is often referred to as a character string.

like image 106
Henrik Avatar answered Oct 24 '25 23:10

Henrik