Basic Data Types

An expression is any construct that an interpreter can evaluate. The there are mainly three types of expressions, corresponding to the basic data types:

  • numerical expressions, correponding to the numerical type (integer and real numbers)
  • textual expressions, corresponding to the character type (characters and strings)
  • logical expressions, corresponding to the boolean types (True or False)

In most languages, the basic data types (numbers, characters, and booleans) are scalars, meaning that they come in units: One can define ONE number, ONE string, or ONE boolean value.

The basic data types are the basic chunk or data that an interpreter can swallow. More complex aggregated data are usually built from these basic data types using the language basic data structures (called also containers, or collections) to aggregate the basic data types. This is not the case in R.

At contrast with most languages, R comes with vectorized basic data types, which are already collections of what, in another language as Python for example, one would call basic data types.

In R, there is nothing such as a single string, a single boolean, or a single number. Evething comes arrange from start in a vector of strings, vector of boolean, or vector of numbers, which, in R lingo, are called:

  • numerical vectors
  • logical vectors
  • character vectors

As in most other linguages, the basic data types have the usual operations defined on them:

  • arithmetic opreations for numerical types
  • boolean operations for boolean types
  • string operations for character types

However, since, in R, the basic types are vectorized, these operations are defined element wise.


In [1]:
%load_ext rmagic

Numbers in R


In [2]:
%%R

x = c(1, 2, 3)
y = c(2, 3, 5)

Caution: In R, all the operations on basic types are vectorized, which means that they are performed element-wise.


In [5]:
%%R

z = x + y
z = x * y
z = x ^ y
#etc. 

print(z)


[1]   1   8 243

Characters in R


In [22]:
%%R
x = rep('Student', 10)
y = sample(1:100, 10)
print(x)
print(y)


 [1] "Student" "Student" "Student" "Student" "Student" "Student" "Student"
 [8] "Student" "Student" "Student"
 [1] 53 47 86 89 60 90 24 44 10 78

In [24]:
%%R
z = paste(x,y, sep=':')
print(z)


 [1] "Student:53" "Student:47" "Student:86" "Student:89" "Student:60"
 [6] "Student:90" "Student:24" "Student:44" "Student:10" "Student:78"

Booleans in R


In [11]:
%%R 

x = rep(T, 10)
y = sample(c(T,F), 10, replace=T)

print(x)
print(y)


 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 [1] FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE

In [18]:
%%R

print(x | y)
print(x & y)
print(!y)


 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 [1] FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
 [1]  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE

In [14]:
%%R

print(any(y))
print(all(y))


[1] TRUE
[1] FALSE

In [ ]: