J. Mini
J. Mini

Reputation: 1610

What are character vectors made of?

"Alice" is a character vector of length 1. "Bob" is also a character vector of length 1, but it's clearly shorter. At face value, it appears that R's character are made out of something smaller than characters, but if you try to subset them, say "Alice"[1], you'll just get the original vector back. How does R internally make sense of this? What are character vectors actually made of?

Upvotes: 1

Views: 303

Answers (1)

Diego Queiroz
Diego Queiroz

Reputation: 394

You're mistaking vector length for string length.

In R common variables are all vectors containing whatever data you typed, so both are vectors that contain 1 string even if you don't assign a name to them.

If you want to check the size of each string, use nchar function:

nchar("Alice")
[1] 5
nchar("Bob")
[1] 3

Upvotes: 1

Related Questions