# 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 :

``````

## Numbers in R

``````

In :

%%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 :

%%R

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

print(z)

``````
``````

   1   8 243

``````

## Characters in R

``````

In :

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

``````
``````

 "Student" "Student" "Student" "Student" "Student" "Student" "Student"
 "Student" "Student" "Student"
 53 47 86 89 60 90 24 44 10 78

``````
``````

In :

%%R
z = paste(x,y, sep=':')
print(z)

``````
``````

 "Student:53" "Student:47" "Student:86" "Student:89" "Student:60"
 "Student:90" "Student:24" "Student:44" "Student:10" "Student:78"

``````

## Booleans in R

``````

In :

%%R

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

print(x)
print(y)

``````
``````

 TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE

``````
``````

In :

%%R

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

``````
``````

 TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE

``````
``````

In :

%%R

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

``````
``````

 TRUE
 FALSE

``````
``````

In [ ]:

``````