R Language: Atomic types: Character
Photo by fauxels from Pexels

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


要查看或添加评论,请登录

Mark Niemann-Ross的更多文章

  • Documenting My Code ... For Me

    Documenting My Code ... For Me

    There are two signs of old age: old age, and ..

  • R Meets Hardware

    R Meets Hardware

    R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data…

    2 条评论
  • Party Buzz Kill: modifying data

    Party Buzz Kill: modifying data

    So Steve (SQL), Marsha (C), Bob (Python), and I (R) are at this party. We have TOTALLY cleared the room, especially now…

    2 条评论
  • Rain - Evapotranspiration = mm Water

    Rain - Evapotranspiration = mm Water

    "Eeee-VAP-oooo-TRANS-PURR-ation," I savor the word as I release it into our conversation. I'm still at the party with…

  • Party Buzz Kill: Data Storage

    Party Buzz Kill: Data Storage

    I'm at this party where Bob and Marsha and I are discussing the best languages for programming a Raspberry Pi. Bob…

    5 条评论
  • R Waters My Garden

    R Waters My Garden

    I'm at a party, and the topic of programming languages comes up. A quarter of the room politely leaves, another half…

    10 条评论
  • Caning and Naming

    Caning and Naming

    We've been back from Port Townsend for a week. Progress on the boat isn't as dramatic as it is when we're spending the…

    1 条评论
  • Irrigate with R and Raspberry Pi

    Irrigate with R and Raspberry Pi

    I’m working on my irrigation system. This requires a controller to turn it on and off.

    3 条评论
  • 5 Reasons to Learn Natural Language Processing with R

    5 Reasons to Learn Natural Language Processing with R

    Why learn R? Why learn Natural Language Processing? Here's five reasons..

    1 条评论
  • Performing Natural Language Processing with R

    Performing Natural Language Processing with R

    I recently released a course on Educative covering topics in Natural Language Processing. Different Learners -…

    1 条评论

社区洞察

其他会员也浏览了