Adam Kurkiewicz
Adam Kurkiewicz

Reputation: 1642

Check the type of a vector variable in R

How can I tell apart a vector of characters from a character in R?

Both typeof(c("a", "b", "c")) and typeof("a") return 'character'.

Upvotes: 3

Views: 9329

Answers (2)

JRR
JRR

Reputation: 3253

In R integer or character or double don't really exist. Everything (or to be more correct, every atomic data) is a vector. Thus if a = 1L, then a is not an integer but a vector of integers with a length of 1.

Both tests return character. You can test the length of the vector.

You can read Data Structures, a chapter of "Adavanced R" to learn more.

Upvotes: 8

Elin
Elin

Reputation: 6770

Mainly because R is a language created to work with data and do statistics everything is considered a vector, matrix or list (there are also constants but set those aside). Vectors and matrices all have classes (or types), which are more similar to what you would see in other languages. What some other languages call strings R calls characters. Character values in a vector or matrix can be any length, they can be a single character "a" or they can be the whole text of War and Peace. This is very different than the idea of a string array (or character array etc) in other languages. This is because people are using these as data to be analyzed in a statistical context.

Upvotes: 1

Related Questions