Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between string and character in R?

Tags:

string

r

I'm not pretty familiar with R, but anyhow I'm writing a R wrapper for a c library. I come across this problem. How do I decide if the input argument is a string? In details,should I write like this:

dyn.load("hello.so")
do_process <- function(str) {
        if(!is.character(str))
            stop("not a character or string");
    result <- .Call("hello", as.character(str))
    return result
}

or this:

dyn.load("hello.so")
do_process <- function(str) {
        if(!is.string(str))
            stop("not a character or string");
    result <- .Call("hello", as.string(str))
    return result
}

Thanks.

like image 544
lulyon Avatar asked Aug 29 '13 14:08

lulyon


People also ask

What is the difference between string and character?

String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char. Strings are immutable. Character Arrays are mutable.

What does character mean in R?

character() function in R converts a numeric object to a string data type or a character object. If the collection is passed to it as an object, it converts all the elements of the collection to a character or string type.

What does strings mean in R?

Strings are a bunch of character variables. It is a one-dimensional array of characters. One or more characters enclosed in a pair of matching single or double quotes can be considered a string in R. Strings represent textual content and can contain numbers, spaces, and special characters.

What is the difference between character and string constant?

The maximum length of a character constant can be one character. A string constant can be any length. A single character string constant has an equivalent integer value.


1 Answers

is.string is a function from the xtable package. In the details section of the help page it explicitly says "These functions are private functions used by print.xtable. They are not intended to be used elsewhere."

As such, I would avoid using those functions.

In R there is no string data type. Instead it is called character and you can use is.character to do the check you're describing.

Also, as I mentioned in my comment, avoid using important base functions as variable names. Specifically str is used to view the structure of an object.

like image 88
Justin Avatar answered Oct 14 '22 00:10

Justin