Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Basic Concepts
The S Language
> 1 + 2
[1] 3
> 1/2
[1] 0.5
> 17^2
[1] 289
Grouping and Evaluation
> 1 + 2 * 3
[1] 7
> (1 + 2) * 3
[1] 9
Operator Precedence
> 2^3^2
[1] 512
> (2^3)^2
[1] 64
> 2^(3^2)
[1] 512
Simple R Expressions
> 1 + 2
[1] 3
> 1/2
[1] 0.5
> 17^2
[1] 289
> 1 + 2 * 3
[1] 7
> (1 + 2) * 3
[1] 9
R Functions
> sqrt(2)
[1] 1.414214
> log(10)
[1] 2.302585
> log10(10)
[1] 1
> sin(1)
[1] 0.841471
> 4 * atan(1)
[1] 3.141593
Assignment
• The statements:
> z = 17
> z <- 17
> 17 -> z
• For example,
> z = 17
> z = z + 23
> z
[1] 40
Numeric Vectors
> x = c(1, 2, 3, 4)
Numeric Vectors
> x = c(1, 2, 3, 4)
> x
[1] 1 2 3 4
Sequences
> 1:50
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13
[14] 14 15 16 17 18 19 20 21 22 23 24 25 26
[27] 27 28 29 30 31 32 33 34 35 36 37 38 39
[40] 40 41 42 43 44 45 46 47 48 49 50
Sequences
> 1:50
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13
[14] 14 15 16 17 18 19 20 21 22 23 24 25 26
[27] 27 28 29 30 31 32 33 34 35 36 37 38 39
[40] 40 41 42 43 44 45 46 47 48 49 50
> 5:-5
[1] 5 4 3 2 1 0 -1 -2 -3 -4 -5
Combining Vectors
> x = c(1, 2, 3, 4)
> c(x, x)
[1] 1 2 3 4 1 2 3 4
> 2 * x + 1
[1] 3 5 7 9
> sqrt(x)
[1] 1.000000 1.414214 1.732051 2.000000
The Recycling Rule
> 11 %% 3
[1] 2
> 1:10 %% 2
[1] 1 0 1 0 1 0 1 0 1 0
> 13.5 %% 2
[1] 1.5
> y = 1:10
> y[4:6] = 0
> y
[1] 1 2 3 0 0 0 7 8 9 10
Special Numerical Values – Infinity
> 1 / 0
[1] Inf
> -1 / 0
[1] -Inf
Special Numerical Values – Infinity
> 1 + Inf
[1] Inf
and
> 0 / 0
[1] NaN
> sqrt(-1)
[1] NaN
Warning message:
In sqrt(-1) : NaNs produced
Special Numerical Values – Not Available
> 1 + sin(NA)
[1] NA
The functions min and max return the minimum and maximum
values contained in any of their arguments, and the function
range returns a vector of length 2 containing the minimum
and maximum of the values in the arguments.
> max(1:100)
[1] 100
> range(1:100)
[1] 1 100
Summary Functions – sum and prod
The functions sum and prod compute the sum and product of
all the elements in their arguments.
> sum(1:100)
[1] 5050
> prod(1:10)
[1] 3628800
Summary Functions and NA
> cumsum(1:10)
[1] 1 3 6 10 15 21 28 36 45 55
> cumprod(1:6)
[1] 1 2 6 24 120 720
> cummax(1:10)
[1] 1 2 3 4 5 6 7 8 9 10
> cummin(1:10)
[1] 1 1 1 1 1 1 1 1 1 1
> b[1:3]
[1] TRUE TRUE FALSE
> 3 < 4
[1] TRUE
> 3 > 4
[1] FALSE
x & y y
TRUE FALSE NA
TRUE TRUE FALSE NA
x FALSE FALSE FALSE FALSE
NA NA FALSE NA
x | y y
TRUE FALSE NA
TRUE TRUE TRUE TRUE
x FALSE TRUE FALSE NA
NA TRUE NA NA
Logical Subsetting
> x = c(1, 2, 3, 4)
> x = 1:10
> ifelse(x > 5, x, -x)
[1] -1 -2 -3 -4 -5 6 7 8 9 10
> x = 1:10
> ifelse(x > 5, x, 0)
[1] 0 0 0 0 0 6 7 8 9 10
Character Vectors
> s[1:2]
[1] "first" "second"
> s
[1] "initial" "second" "third"
> nchar(s)
[1] 7 6 5
> z = 10+20i
> z
[1] 10+20i
> z = -1+0i
> sqrt(z)
[1] 0+1i
Vector Mode and Length
> numeric(5)
[1] 0 0 0 0 0
Creating Vectors
> vector("numeric", 5)
[1] 0 0 0 0 0
> vector("logical", 5)
[1] FALSE FALSE FALSE FALSE FALSE
> numeric(0)
numeric(0)
> x = 1:10
> v = 1:4
> names(v) = c("A", "B", "C", "D")
> v
A B C D
1 2 3 4
> names(v)
[1] "A" "B" "C" "D"
Subsetting Using Names
> v = c(1, 2, 3, 4)
> names(v) = c("A", "B", "C", "D")
> v["A"]
A
1
This list has three elements. The first element is the number
10, the second is the character string "eleven" and the third
is the logical value TRUE.
Printing Lists
> lst
[[1]]
[1] 10
[[2]]
[1] "eleven"
[[3]]
[1] TRUE
Named Lists
$b
[1] "eleven"
[[3]]
[1] TRUE
Elements and Subsets of Lists
> lst[[1]]
[1] 10
> lst[1]
[[1]]
[1] 10
The first result here is the element 10 extracted from the list.
The second is a list containing that element.
Extracting Named Elements
> lst$a
[1] 10
> length(NULL)
[1] 0
> as.numeric(NULL)
numeric(0)
> as.list(NULL)
list()