Basic Atomic Variables in R Programming

There are five basic atomic variables in R Programming.

Being new to the language, the first thing that threw me for a loop was that the assignment operator is “<-“. I’ve professionally programmed in C, C++, Perl, PHP, JavaScript, C#, Java, Visual Basic, and a few other languages; the “<-” for assignment is definitely an outlier.

Below are syntax and observation notes for the core atomic variables in R Programming. Detailed documentation exists at https://cran.r-project.org/.

Integer

To declare an integer, the syntax is:

x <- 2L

The explicit typecasting is required because R defaults to the double data type.

The data type of a variable can be verified by using the typeof function:

typeof(x)

This is a commonly supported function. In Python it’s “type”, in JavaScript it’s “typeof”, and so on.

Double

As noted above, the default type for number values is double. That is the case whether or not there is a decimal place. Note that Python’s default is single precision float when numbers contain a fractional component.

y <- 2.5

Complex

R has a complex variable type by default too (i.e. Real number)

compl <- 3 + 2i

I’ve never used complex numbers in computations (haven’t thought about them since college). Perhaps I’ll have an opportunity as I explore R Programming further and continue my project work in Bio Tech.

Character

This one is interesting:

  • Everything in JavaScipt is a string, whether you use single or double quotes..
  • In C, C++, and C#…the use of double quotes represents string and single quotes is character
  • In Python, everything is a string whether you use single or double quotes.

R, however, defaults everything to character whether it has single or double quotes.

#character type

a <- ‘string’
typeof(a)

#also a character type

strv = “string”
typeof(strv)

Logical

This is also the first programming language I’ve seen “boolean’ referred to as “logical”. Also, the assignment can either be T, TRUE, F, or FALSE.

I’m curious to see what I’ll learn further down the road about how R handles falsey/truthy and nullish logic.

q <- TRUE
typeof(q)

j <- F
typeof(j)

R Studio Global Environment Watch Window

Something interesting is the default prominence of the watch window in R Studio:

R Studio Environment Window

The environment watch window is not visible by default in Visual Studio Code, Eclipse, and other IDEs at runtime. It becomes part of the default experience during debug time, but at runtime, one must set the IDE preferences to make it visible. In R Studio, it’s visible as part of the default layout.



Categories: R Programming

Tags: ,

Leave a Reply

%d bloggers like this: