[Rd] Clarifications please.

Simon Urbanek simon.urbanek at r-project.org
Fri Aug 28 16:10:44 CEST 2009


On Aug 28, 2009, at 9:42 AM, Abhijit Bera wrote:

> Hi Simon
>
> Thanks. All the confusion between CHAR and STRING. First I read this :
>
> http://www1.cuni.cz/~obo/r_surprises.html
>

Uh, oh, that doc confuses things even more and is not really correct  
(well, the author admits that he has no idea, but that doesn't help).  
Ok, so a quick clarification of the world of strings and character  
vectors.

In R speak string vector is a vector containing 0 or more character  
strings (e.g. c("foo","bar")). It is sometimes also called character  
vector, because it is of type and mode "character". In the R C-level  
API string vectors are represented by the type STRSXP and you can use  
LENGTH(), STRING_ELT(x,i) and SET_STRING_ELT(x,i,a) macros on it to  
get the length and get/set elements. The elements are character  
strings which are represented by CHARSXP (this is not a vector in R  
land) and are always read-only.  They can be created using mkChar*()  
functions and the C-level const char* content (\0-terminated for now)  
can be retrieved using CHAR(x).

The author of the above page is confused about mkString - which  
creates a string vector (STRSXP) and mkChar which creates a character  
string (CHARSXP). Essentially mkString(x) is just a shortcut for  
v=allocVector(STRSXP, 1); SET_STRING_ELT(v, 0, mkChar(x)); x; Users in  
R never see CHARSXP directly - it is never used when interfacing  
another function since there are conceptually no scalars in the R  
language.

I hope this helps to sort out the confusion.

Cheers,
Simon


> In the meantime I got your reply. Then I read the documentation and  
> it made sense!
>
> Regards
>
> Abhijit Bera
>
> [..snip, previously posed code omitted ..]



More information about the R-devel mailing list