R Language: Atomic types: Character
If you're using R, you should have a good grasp of the atomic data types. It's like practicing a musical instrument - you need to keep the basics fresh. So let's look at the character atomic type...
R views numerics and characters differently. For instance...
> I.am.character <- "I like R"
> as.numeric(I.am.character)
[1] NA
Warning message:
NAs introduced by coercion?
Attempting to convert a character to a numeric results in NA. What about converting a number to a character? This gets a bit more confusing...
> as.character(3.14) == as.numeric(3.14)
[1] TRUE
The takeaway; R will try to do it's best when creating data types, but it's possible to create ambiguous values.
Another common situation is assuming a character string is like an array. In some other languages this is true, but what happens when you try to subset a character?
> I.am.character[3]
[1] NA
You would think this might produce "l" - the 3rd character of "I like R." Instead, it produces NA. This is because R uses vectors - not arrays. I.am.character is only one item long. length of a vector is different than nchar ...
> nchar(I.am.character) != length(I.am.character)?
[1] TRUE
If you want the third character of the object, use substr() instead...
> substr(I.am.character,start=3,stop=3) # use substr instead
[1] "l"
Take a minute to understand why this works - and you'll understand the character atomic better.
by the way...
I teach R on LinkedIn Learning. Look me up for more weekly R goodness...