Introduction to R - Introduzione a R

R objects

R objects types are called modes. In the next paragraphs we will see different R modes. The simplest R objects and their modes are:

  • MODE: NUMERIC (numeric [integer, real] or complex)

> a=10
> b=15.53
> d=3.51+1.2i

check the mode of the objects by typing

> mode(a)
[1] "numeric"
> mode(b)
[1] "numeric"
> mode(d)
[1] "complex"

  • MODE: CHARACTERS

> e="Hallo"
> f="World"
> mode(e)
[1] "character"

  • MODE: LOGICAL

> g=TRUE
> h=FALSE
> i=F
> l=T
> mode(g)
[1] "logical"

  • VECTORS: Vectors are created using the concatenating function c. For example a vector of numbers

> v1=c(5,2,3,1,9)
> v1
[1] 5 2 3 1 9
> mode(v1)
[1] "numeric"

A vector of characters

> v2=c("a","b","c")
> v2
[1] "a" "b" "c"
> mode(v2)
[1] "character"

A vector of "objects"

> v3=c(a,b,d)
> v4=c(g,h,i,l)
> mode(v4)
[1] "logical"

and an empty vector

> v5=c()

Individual elements of a vector are accessed via [position of the element]. For example to select the second element of v1, type

> v1[2] [1] 2

Subsetting is a very important operation on vectors. For example to select a subset of consecutive elements of the vector type [position of the first element:position of the last element]. For example, if you want to select from the second to the fourth element of v1

> v1[2:4]
[1] 2 3 1
> v1
[1] 5 2 3 1 9

To select elements in non consecutive positions type their positions in the vector using the c() function. For example, to select elements of position 3,2,5 of v1

> v1[c(3,2,5)]
[1] 3 2 9

to know how many elements are in a vector type

> length(v1) [1] 5

Simple operations on vector can be carried on such as:

> sum(v1)
[1] 20
> mean(v1)
[1] 4
> prod(v1)
[1] 270
> cumsum(v1)
[1] 5 7 10 11 20
> v1-2
[1] 3 0 1 -1 7
> v1/2
[1] 2.5 1.0 1.5 0.5 4.5

When applying an operation to two vectors that requires them to be of the same length, R automatically recycles, or repeats, the shorter one, until it is long enough to match the longer one

> c(2,4,1)+c(5,1,2,0,0,4)
[1] 7 5 3 2 4 5

that is equal to

> c(2,4,1,2,4,1)+c(5,1,2,0,0,4)
[1] 7 5 3 2 4 5
> v1*c(5,2)
[1] 25 4 15 2 45

equivalent to

> v1*c(5,2,5,2,5)
[1] 25 4 15 2 45

Vectors contain objects of the same mode. If one concatenates elements of different modes, for example numeric and character the result is

> new.1=c(v1,v2)
> mode(new.1)
[1] "character"
> new.2=c(v1,v2,v3,g,h,v4)
> new.2

[1] "5" "2" "3" "1" "9" "a" "b" "c"
[9] "10+0i" "15.53+0i" "3.51+1.2i" "TRUE" "FALSE" "TRUE" "FALSE" "FALSE"
[17] "TRUE"

> mode(new.2)
[1] "character"

It is always possible to change the mode of a vector

> as.character(v1)
[1] "5" "2" "3" "1" "9"

> as.character(v3)
[1] "10+0i" "15.53+0i" "3.51+1.2i"

> as.character(v4)
[1] "TRUE" "FALSE" "FALSE" "TRUE"

> as.numeric(v4)
[1] 1 0 0 1

> as.numeric(v2)
[1] NA NA NA

  • MODE: LIST One simple solution to put elements of different modes and lengths together without changing their modes or reciclying elements is the list. A list is a container for items of different data types. For example:

> list.1=list(c("Hallo","Friends","Hallo"),c(30,50,0.5,0.1),c(TRUE,TRUE,FALSE))
> list.1

[[1]]
[1] "Hallo" "Friends" "Hallo"

[[2]]
[1] 30.0 50.0 0.5 0.1

[[3]]
[1] TRUE TRUE FALSE

> mode(list.1) [1] "list"

To access list elements we have two solutions.
Solution 1: if the elements of the list do not have a name as in list.1, just type the position using [[]]

> list.1[[1]]
[1] "Hallo" "Friends" "Hallo"

> list.1[[1]][1]
[1] "Hallo"

> list.1[[1]][2]
[1] "Friends"

> list.1[[1]][c(1,3)]
[1] "Hallo" "Hallo"

> list.1[[2]]
[1] 30.0 50.0 0.5 0.1

> list.1[[2]][4]
[1] 0.1

> list.1[[2]][1:3]
[1] 30.0 50.0 0.5

> list.1[[2]][c(1,4)]
[1] 30.0 0.1

Solution 2: if the elements of the list have a name, you can directly access them using $

> list.2=list(welcome=c("Hallo","Friends","Hallo"),
+ mynumbers=c(30,50,0.5,0.1),
+ myidea=c(TRUE,TRUE,FALSE))
> list.2
$welcome
[1] "Hallo" "Friends" "Hallo"

$mynumbers
[1] 30.0 50.0 0.5 0.1

$myidea
[1] TRUE TRUE FALSE

> list.2[[1]]
[1] "Hallo" "Friends" "Hallo"

> # is equivalent to
> list.2$welcome
[1] "Hallo" "Friends" "Hallo"

> list.2[[1]][c(1,3)]
[1] "Hallo" "Hallo"

> list.2$welcome[c(1,3)]
[1] "Hallo" "Hallo"

Gli oggetti di R

I tipi di oggetto di R sono chiamati "modi". Nei seguentei paragrafi si vedranno diversi "modi" in R. Gli oggetti piú semplici ed i loro modi in R sono:

  • MODO: NUMERICO (numeric [integer, real] or complex)

> a=10
> b=15.53
> d=3.51+1.2i

si verifica il modo di un oggetto digitando mode

> mode(a)
[1] "numeric"
> mode(b)
[1] "numeric"
> mode(d)
[1] "complex"

  • MODO: CARATTERI

> e="Hallo"
> f="World"
> mode(e)
[1] "character"

  • MODO: LOGICO

> g=TRUE
> h=FALSE
> i=F
> l=T
> mode(g)
[1] "logical"

  • VETTORI: I vettori sono creati utilizzando la funzione di concatenazione c(). Per esempio un vettore di numeri

> v1=c(5,2,3,1,9)
> v1
[1] 5 2 3 1 9
> mode(v1)
[1] "numeric"

Un vettore di caratteri

> v2=c("a","b","c")
> v2
[1] "a" "b" "c"
> mode(v2)
[1] "character"

Un vettore di "oggetti"

> v3=c(a,b,d)
> v4=c(g,h,i,l)
> mode(v4)
[1] "logical"

e un vettore vuoto

> v5=c()

I singoli elementi di un vettore vengono richiamati utilizzando [position of the element]. Per selezionare il secondo elemento del vettore v1, si digita

> v1[2] [1] 2

Una operazione importante sui vettori é l'estrazione di sottoinsiemi. Per esempio per selezionare un sottoinsieme di elementi consecutivi di un vettore si utilizza [posizione del primo elemento:posizione dell'ultimo elemento]. Per esempio, se si vuole selezionare dal secondo al quarto elemento del vettore v1

> v1[2:4]
[1] 2 3 1
> v1
[1] 5 2 3 1 9

Per selezionare elementi in posizioni non consecutive é sufficiente digitare la loro posizione nel vettore utilizzare la funzione c(). Per esempio, per selezionare gli elementi nelle posizioni 3,2,5 di v1

> v1[c(3,2,5)]
[1] 3 2 9

per conoscere il numero di elementi in un vettore digitare

> length(v1) [1] 5

Si possono eseguire operazioni semplici sui vettori, come:

> sum(v1)
[1] 20
> mean(v1)
[1] 4
> prod(v1)
[1] 270
> cumsum(v1)
[1] 5 7 10 11 20
> v1-2
[1] 3 0 1 -1 7
> v1/2
[1] 2.5 1.0 1.5 0.5 4.5

Se si effettuano operazioni fra due vettori é necessario che siano della stessa grandezza. R automaticamente ripete il vettore piú piccolo fino a quando non ha la stessa lunghezza dell'altro

> c(2,4,1)+c(5,1,2,0,0,4)
[1] 7 5 3 2 4 5

che é uguale a

> c(2,4,1,2,4,1)+c(5,1,2,0,0,4)
[1] 7 5 3 2 4 5
> v1*c(5,2)
[1] 25 4 15 2 45

é equivalente a

> v1*c(5,2,5,2,5)
[1] 25 4 15 2 45

Vettori contengono oggetti dello stesso modo (tipo). Se si concatenano elemnti di modi diversi, come ad esempio numerico e carattere il risultato sará

> new.1=c(v1,v2)
> mode(new.1)
[1] "character"
> new.2=c(v1,v2,v3,g,h,v4)
> new.2

[1] "5" "2" "3" "1" "9" "a" "b" "c"
[9] "10+0i" "15.53+0i" "3.51+1.2i" "TRUE" "FALSE" "TRUE" "FALSE" "FALSE"
[17] "TRUE"

> mode(new.2)
[1] "character"

É sempre possibile cambiare il modo di un vettore

> as.character(v1)
[1] "5" "2" "3" "1" "9"

> as.character(v3)
[1] "10+0i" "15.53+0i" "3.51+1.2i"

> as.character(v4)
[1] "TRUE" "FALSE" "FALSE" "TRUE"

> as.numeric(v4)
[1] 1 0 0 1

> as.numeric(v2)
[1] NA NA NA

  • MODO: LISTA Una soluzione semplice per unire elementi di modi e di lunghezze diverse senza cambiare il loro modo o ripetendo gli elementi é la lista (list). Una lista é un contenitore di elementi di tipi di dati differenti. Per esempio:

> list.1=list(c("Hallo","Friends","Hallo"),c(30,50,0.5,0.1),c(TRUE,TRUE,FALSE))
> list.1

[[1]]
[1] "Hallo" "Friends" "Hallo"

[[2]]
[1] 30.0 50.0 0.5 0.1

[[3]]
[1] TRUE TRUE FALSE

> mode(list.1) [1] "list"

Per accedere agli elemnti di una lista ci sono due modalitá.
Modalitá 1: se gli elementi della lista non hanno nome, come nella lista list.1, é sufficiente indicare la posizione utilizzando [[]]

> list.1[[1]]
[1] "Hallo" "Friends" "Hallo"

> list.1[[1]][1]
[1] "Hallo"

> list.1[[1]][2]
[1] "Friends"

> list.1[[1]][c(1,3)]
[1] "Hallo" "Hallo"

> list.1[[2]]
[1] 30.0 50.0 0.5 0.1

> list.1[[2]][4]
[1] 0.1

> list.1[[2]][1:3]
[1] 30.0 50.0 0.5

> list.1[[2]][c(1,4)]
[1] 30.0 0.1

Modalitá 2: se gli elementi hanno un nome, si puó accedere agli elementi direttamente dal nome utilizzando $

> list.2=list(welcome=c("Hallo","Friends","Hallo"),
+ mynumbers=c(30,50,0.5,0.1),
+ myidea=c(TRUE,TRUE,FALSE))
> list.2
$welcome
[1] "Hallo" "Friends" "Hallo"

$mynumbers
[1] 30.0 50.0 0.5 0.1

$myidea
[1] TRUE TRUE FALSE

> list.2[[1]]
[1] "Hallo" "Friends" "Hallo"

> # is equivalent to
> list.2$welcome
[1] "Hallo" "Friends" "Hallo"

> list.2[[1]][c(1,3)]
[1] "Hallo" "Hallo"

> list.2$welcome[c(1,3)]
[1] "Hallo" "Hallo"