Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the type of a vector variable in R

Tags:

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'.

like image 419
Adam Kurkiewicz Avatar asked Oct 26 '25 10:10

Adam Kurkiewicz


2 Answers

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.

like image 177
JRR Avatar answered Oct 29 '25 01:10

JRR


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.

like image 43
Elin Avatar answered Oct 29 '25 00:10

Elin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!