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

R Language: Atomic types: Complex

If you're going to program in R, then you need to understand the atomic data types: logical, integer, real, complex, character and raw. Let's look at complex numbers in R.

First, a quick review. Complex numbers are a combination of a real number and an imaginary number. Real numbers are easy: integers and fractionals are real numbers. Imaginary numbers are also easy(?): the square root of -1 (sort of).

How do you write a complex number in R? Here's some code...

> I.am.complex <- 3i
> class(I.am.complex)
[1] "complex"

> I.am.complex <- 1 + 2i

> I.am.complex <- -1 + 0i

> as.complex(3)
[1] 3+0i

        

Just add the letter "i" to a number - or use as.complex(). Most R functions know how to handle complex (and imaginary) numbers.

> sqrt(I.am.complex)
[1] 0+1i

> -1+0i == as.complex(-1)
[1] TRUE

> as.complex(3)
[1] 3+0i
        

Easy and quick!

Of course, what you do with complex numbers is your deal. Let me know if you depend on them.

Need more? Here's the R weekly session on atomic types...

By the way...

I cover stuff like this every week. Check out R for Data Science: Lunchbreak Lessons.

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

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 条评论

社区洞察

其他会员也浏览了