4 ms·
You're right in the sense that there is no fundamental difference between character and string type in R. A "string" is just a character variable which contains
by phillc73 9y ago
You're right in the sense that there is no fundamental difference between character and string type in R. A "string" is just a character variable which contains more than one letter.
variable_name <- "This is a string"
# These all tell you the class type
str(variable_name)
class(variable_name)
typeof(variable_name)
# Is variable character type? Boolean TRUE/FALSE
is.character(variable_name)
# This tells you the number of characters. > 1 is a string.
nchar(variable_name)
# With vector this time
variable_char_vector <- c("String A", "String B")
# How long?
length(variable_char_vector)
# What types?
str(variable_char_vector)
# Are they strings?
nchar(variable_char_vector)
I'm not an expert in a wide range of programming languages, but this doesn't seem to be a major shortcoming in R.
- stewbrew 9y agoMany lines that don't solve the problem. What I meant: write a boolean test (a function that returns true if x is a string (a "character" vector of length 1) and false otherwise) that never throws an error.
- phillc73 9y agoAppreciate this is not Stackoverflow, and happy to take the discussion over there if you would like to raise a question. However, the function I would write is: string_test <- function(x){ is.character(x) && length(x) == 1 } If you can say where that will fail, I'd like to know because it seems like you probably have more experience than I do around this.