Is there a way to determine if an R variable is a single string?
is.character
looked promising, but there was one issue:
is.character(c("a", "b"))
also returned TRUE
which is not what I want.
character() Function in R Language is used to check if the object is of the form of a string/character or not. It will return true if any element of the object is of the character data type.
Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string') . If the typeof operator returns "string" , then the variable is a string. In all other cases the variable isn't a string. Copied!
In R, we use the grepl() function to check if characters are present in a string or not. And the method returns a Boolean value, TRUE - if the specified sequence of characters are present in the string.
Based on the comments, this is my current solution:
isSingleString <- function(input) {
is.character(input) & length(input) == 1
}
Try is.string()
function from assertthat
package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With