R - Determine if a variable is a string
Asked Answered
C

2

12

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.

Complement answered 14/7, 2016 at 23:0 Comment(4)
One way would be checking the length at the same time.Thermotensile
Maybe is.character(c("a", "b")) & length(c("a", "b")) == 1Punk
or class(c("a","b")) & length(c("a","b"))==1. Note there's no such thing as a variable in R really. c("a","b") is really 2 different objects in a function that prepares them for assignment to a vector or list, both of which are character, i.e. string.Kyne
? both class and is.character are built-in. when would they not work?Kyne
C
13

Based on the comments, this is my current solution:

isSingleString <- function(input) {
    is.character(input) & length(input) == 1
}
Complement answered 14/7, 2016 at 23:16 Comment(0)
E
3

Try is.string() function from assertthat package.

Eisk answered 22/8, 2018 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.