Introduction

Assignment of a variable:

x = 1
x <- 1 
(X = 2)
[1] 2
x
[1] 1
X
[1] 2

Help:

?plot
Help on topic 'plot' was found in the following packages:

  Package               Library
  graphics              /usr/lib/R/library
  base                  /usr/lib/R/library


Utilisation de la première correspondance ...

Logic:

Logic: R
AND &
OR |
NO !
TRUE T ou TRUE
FALSE F ou FALSE
\(\neq\) !=
\(\geq\) >=
\(=\) ==
1+2==3
[1] TRUE
!((3>1) & (2>=2))
[1] FALSE

Vector

Entering a vector:

1:10
 [1]  1  2  3  4  5  6  7  8  9 10
c(1,7,12,-1)
[1]  1  7 12 -1
seq(from=1, to=10, by=0.1)
 [1]  1.0  1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8  1.9  2.0  2.1  2.2  2.3  2.4
[16]  2.5  2.6  2.7  2.8  2.9  3.0  3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9
[31]  4.0  4.1  4.2  4.3  4.4  4.5  4.6  4.7  4.8  4.9  5.0  5.1  5.2  5.3  5.4
[46]  5.5  5.6  5.7  5.8  5.9  6.0  6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9
[61]  7.0  7.1  7.2  7.3  7.4  7.5  7.6  7.7  7.8  7.9  8.0  8.1  8.2  8.3  8.4
[76]  8.5  8.6  8.7  8.8  8.9  9.0  9.1  9.2  9.3  9.4  9.5  9.6  9.7  9.8  9.9
[91] 10.0
seq(0, 1, length=10)
 [1] 0.0000000 0.1111111 0.2222222 0.3333333 0.4444444 0.5555556 0.6666667
 [8] 0.7777778 0.8888889 1.0000000
rep(1:3, times=2)
[1] 1 2 3 1 2 3
rep(1:3, times=2, each=2)
 [1] 1 1 2 2 3 3 1 1 2 2 3 3
rep(1:3, times=c(2,7,3))
 [1] 1 1 2 2 2 2 2 2 2 3 3 3

Numeric, character or boolean values :

(x=c(0,pi,-2.3,sqrt(7)))
[1]  0.000000  3.141593 -2.300000  2.645751
(x1=c(x,NA))
[1]  0.000000  3.141593 -2.300000  2.645751        NA
(y=c('Vietnam','France','Orleans'))
[1] "Vietnam" "France"  "Orleans"
(z=c(T,F,F,TRUE,FALSE,1==2,2<3))
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE
mode(x)
[1] "numeric"
mode(y)
[1] "character"
mode(z)
[1] "logical"
length(x)
[1] 4
length(y)
[1] 3
length(z)
[1] 7
is.numeric(x)
[1] TRUE
is.character(y) 
[1] TRUE
is.logical(x>1)
[1] TRUE
is.vector(x)
[1] TRUE
is.matrix(x)
[1] FALSE
is.null(x)
[1] FALSE
is.na(x1)
[1] FALSE FALSE FALSE FALSE  TRUE

Usual operations:

x + 2
[1]  2.000000  5.141593 -0.300000  4.645751
x>=1
[1] FALSE  TRUE FALSE  TRUE
x^2
[1] 0.000000 9.869604 5.290000 7.000000
sqrt(abs(x))
[1] 0.000000 1.772454 1.516575 1.626577

Name the components of a vector:

score=c(10,7,14,3)
lecture=c('Phy','Mat','Eco','Info')
names(score)=lecture
score
 Phy  Mat  Eco Info 
  10    7   14    3 
names(score)=NULL

score=c(Phy=10,Mat=7,Eco=14,Info=3)
score
 Phy  Mat  Eco Info 
  10    7   14    3 

Sort the components of a vector:

(x=c(pi,0,1,-3.2,(1+sqrt(5))/2))
[1]  3.141593  0.000000  1.000000 -3.200000  1.618034
sort(x)
[1] -3.200000  0.000000  1.000000  1.618034  3.141593
sort(x,decreasing = T)
[1]  3.141593  1.618034  1.000000  0.000000 -3.200000
sort(x,dec = T)
[1]  3.141593  1.618034  1.000000  0.000000 -3.200000
order(x)
[1] 4 2 3 5 1
x[order(x)]
[1] -3.200000  0.000000  1.000000  1.618034  3.141593

Select components of a vector:

x[2]
[1] 0
x[-2]
[1]  3.141593  1.000000 -3.200000  1.618034
x[c(2,4)]
[1]  0.0 -3.2
x[x>=1]
[1] 3.141593 1.000000 1.618034
lecture[score>=10]
[1] "Phy" "Eco"

Concatenation :

x0 = c(1,2)
y0 = c(3,4)
(z = c(x0,y0))
[1] 1 2 3 4

Matrix

Entering a matrix:

A = matrix(c(5,2,-1,3,-4,6), nrow=2, ncol=3)
B = matrix(c(5,2,-1,3,-4,6), nrow=2, ncol=3, byrow=T)

Equality test:

1 == 2
[1] FALSE
x == 1
[1] FALSE FALSE  TRUE FALSE FALSE
A == B
      [,1]  [,2]  [,3]
[1,]  TRUE FALSE FALSE
[2,] FALSE FALSE  TRUE
any(A == B)
[1] TRUE
all(A == B)
[1] FALSE

Coefficient in position (i, j):

A[2,3]
[1] 6

Extracting a row/column:

A[2,]
[1] 2 3 6
A[,1]
[1] 5 2

Deleting a row/column (or several rows/columns):

A[-2,]
[1]  5 -1 -4
A[,-1]
     [,1] [,2]
[1,]   -1   -4
[2,]    3    6
A[-2,-1]
[1] -1 -4
A[-2,c(-1,-2)]
[1] -4

Transpose a matrix :

t(A)
     [,1] [,2]
[1,]    5    2
[2,]   -1    3
[3,]   -4    6

Usual operations:

A + B
     [,1] [,2] [,3]
[1,]   10    1   -5
[2,]    5   -1   12
A * B
     [,1] [,2] [,3]
[1,]   25   -2    4
[2,]    6  -12   36
A %*% t(B)
     [,1] [,2]
[1,]   27   -5
[2,]   10   30
A + 3
     [,1] [,2] [,3]
[1,]    8    2   -1
[2,]    5    6    9
3*A
     [,1] [,2] [,3]
[1,]   15   -3  -12
[2,]    6    9   18
A^3
     [,1] [,2] [,3]
[1,]  125   -1  -64
[2,]    8   27  216
2^A
     [,1] [,2]    [,3]
[1,]   32  0.5  0.0625
[2,]    4  8.0 64.0000

Concatenation :

rbind(A, B)
     [,1] [,2] [,3]
[1,]    5   -1   -4
[2,]    2    3    6
[3,]    5    2   -1
[4,]    3   -4    6
cbind(A, B)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    5   -1   -4    5    2   -1
[2,]    2    3    6    3   -4    6

Special case of square matrices

Identity matrix of given format:

diag(4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

Diagonal matrix formed from given coefficients:

(C=diag(c(2,-1,5,0,8)))
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    0    0    0    0
[2,]    0   -1    0    0    0
[3,]    0    0    5    0    0
[4,]    0    0    0    0    0
[5,]    0    0    0    0    8

Determinant of a matrix

det(C)
[1] 0

Diagonal elements of a matrix:

diag(A)
[1] 5 3

Inverse :

A =matrix(c(6,12,3,5,10,5,3,8,2),3,3)
B =matrix(c(2,3,3,4,6,5,6,9,5),3,3)
solve(A)
           [,1]       [,2]       [,3]
[1,]  0.6666667 -0.1666667 -0.3333333
[2,]  0.0000000 -0.1000000  0.4000000
[3,] -1.0000000  0.5000000  0.0000000
solve(A, B) 
           [,1]          [,2]       [,3]
[1,] -0.1666667 -1.480297e-16  0.8333333
[2,]  0.9000000  1.400000e+00  1.1000000
[3,] -0.5000000 -1.000000e+00 -1.5000000
solve(A) %*% B
           [,1]          [,2]       [,3]
[1,] -0.1666667 -5.551115e-17  0.8333333
[2,]  0.9000000  1.400000e+00  1.1000000
[3,] -0.5000000 -1.000000e+00 -1.5000000

Random variable

Generate data

y = runif(1000)
y[1:30]
 [1] 0.835938710 0.428667026 0.407574144 0.006063845 0.977409488 0.262921007
 [7] 0.537516835 0.885083936 0.266330516 0.108101886 0.342384226 0.899514456
[13] 0.142731028 0.448430820 0.460109022 0.808785409 0.747074644 0.452997989
[19] 0.106499520 0.566914315 0.126383577 0.109943035 0.279621152 0.126814258
[25] 0.328277479 0.507362241 0.860770951 0.174909196 0.687567855 0.182753417
hist(y,freq=F)

y = runif(1000,2,3)
y[1:30]
 [1] 2.752475 2.607184 2.652447 2.064841 2.254607 2.348022 2.236251 2.834992
 [9] 2.753475 2.672690 2.205300 2.807413 2.683947 2.251402 2.983518 2.011137
[17] 2.554966 2.957088 2.597718 2.236718 2.733337 2.279272 2.448419 2.350797
[25] 2.221219 2.709922 2.348577 2.507301 2.180082 2.471986
hist(y,freq=F)

z = rnorm(1000)
hist(z,freq=F)

z = rnorm(1000,10,2)
hist(z,freq=F)

?distributions 

Probability/Density Function

t=0:7
plot(t,dbinom(t,7,0.2),type='h')

t = seq(-7, 7, by=0.1)
f = dnorm(t)
plot(t,f,type='l')

Cumulative Distribution Function

t=0:7
plot(t,pbinom(t,7,0.2),type='s')

t = seq(-7, 7, by=0.1)
Fc = pnorm(t)
plot(t,Fc,type='l')

Quantile

qbinom(0.7,7,0.2)
[1] 2
t = seq(-7, 7, by=0.1)
Fc = pnorm(t)
plot(t,Fc,type='l')
q=qnorm(0.95)
segments(-7,0.95,q,0.95,col='orange')
segments(q,0,q,0.95,col='orange',lty=3)

f = dnorm(t)
plot(t,f,type='l')
value <- qnorm(0.95)
tq=max(which(t < value))
polygon(c(t[c(1, 1:tq, tq)]),
        c(0,f[1:tq], 0),
        col = 'orange')
legend("topright",
       legend = c(0.95),
       fill =  'orange') 

qnorm(0.95)
[1] 1.644854

Dataframe

x1=rnorm(100,2,10)
x2=rgeom(100,0.2)
x3=sample(0:9,100,replace = T)
Dataframe=data.frame(X=x1,Y=x2,Z=x3,row.names = NULL)
Dataframe$X
  [1]  -8.79180921   4.13471722  10.36440021   1.14869767  14.66433955
  [6]   3.08886560   9.48671653   7.05361537  -9.16318029   9.63624993
 [11]   5.85390602  14.97642312 -12.59176181  11.80516142  31.25309838
 [16]   0.98577879 -14.52082820  -8.27589227   6.90053945   6.48928780
 [21]  24.51464902  11.70847706  10.80784501  -1.24622589   6.25858449
 [26]   3.18786801  10.05446118   6.57528854   4.63030946  -6.13363762
 [31]   7.76419265  10.74789321  -4.35830369  -9.90536586  18.38971537
 [36]   4.24995241   3.13299987  -4.81719734  -0.36543800  -5.69964350
 [41] -14.68430813 -22.24708705 -10.31430450  26.68027923   9.57073887
 [46]   5.13069787   9.42859940  14.23796475   4.42070750  -1.25144451
 [51]  14.79658001  -0.31301560  -2.91630942  -9.22491248   5.68602218
 [56]  13.86216027   3.54142212  -1.80050111   6.87268531   1.65582584
 [61]  -5.19539324  10.78596350   6.28597619  -1.87146587  10.99575058
 [66]  16.38924685  10.04237980  -0.06370955 -16.39620292  -1.38737191
 [71]   0.74026802   2.14401918 -14.83989006  21.64705243  -5.24545651
 [76]  -9.82616070 -10.92668815   6.28421012 -17.97047621  -3.59247728
 [81]   0.79022359   9.83097339   5.59893426  -9.22652104   2.07302804
 [86]  -6.30443179  18.21061418   9.71794961 -24.55376849  -3.90530043
 [91]  20.39071365   5.50290413  11.24100411   8.66959041  14.39227432
 [96]   3.98247038  -2.64719098  -9.11293864  -7.12800396   1.52292897
Dataframe[10,]
         X Y Z
10 9.63625 9 0

Data sets

From R

data()
data(trees)
?trees
attach(trees)
Height
 [1] 70 65 63 72 81 83 66 75 80 75 79 76 76 69 75 74 85 86 71 64 78 80 74 72 77
[26] 81 82 80 80 80 87
Volume
 [1] 10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 24.2 21.0 21.4 21.3 19.1
[16] 22.2 33.8 27.4 25.7 24.9 34.5 31.7 36.3 38.3 42.6 55.4 55.7 58.3 51.5 51.0
[31] 77.0
Girth
 [1]  8.3  8.6  8.8 10.5 10.7 10.8 11.0 11.0 11.1 11.2 11.3 11.4 11.4 11.7 12.0
[16] 12.9 12.9 13.3 13.7 13.8 14.0 14.2 14.5 16.0 16.3 17.3 17.5 17.9 18.0 18.0
[31] 20.6
plot(Girth, Volume)

From a file

data=read.table(file='Students.txt',header=T)
data
           Transport Score Brotherhood         Time
1   Public_Transport     F           3  0.622526462
2                Car     F           4 36.657511927
3   Public_Transport     C           3 11.522504011
4   Public_Transport     D           1 33.828593223
5               Bike     D           0  9.060720704
6   Public_Transport     F           4  4.857579940
7   Public_Transport     A           5 11.671969750
8   Public_Transport     C           2 18.826682988
9                Car     D           4  1.900738537
10  Public_Transport     A           4 12.803518513
11               Car     D           1  2.836075382
12  Public_Transport     D           2 20.945708765
13              Walk     F           2 11.724335026
14         Motorbike     C           2 21.810021646
15  Public_Transport     C           5 34.394449328
16               Car     C           2 41.642181424
17               Car     B           4  2.348245194
18               Car     D           2 16.857141836
19  Public_Transport     B           3  5.928522913
20               Car     C           3 28.115604846
21              Walk     D           2  1.306014591
22  Public_Transport     B           1 86.365817737
23  Public_Transport     B           1 50.441392478
24              Bike     B           2 30.058228249
25  Public_Transport     B           3 24.271936752
26               Car     C           1  4.105579573
27               Car     D           3 47.241244459
28              Bike     D           0  0.007837325
29               Car     D           2  2.906950905
30              Bike     C           1 11.342407078
31              Walk     C           3 23.560583102
32  Public_Transport     F           3 26.990008915
33  Public_Transport     D           2 42.449839111
34              Bike     C           3  7.878798416
35         Motorbike     A           4  7.898013091
36               Car     D           2 22.147315960
37  Public_Transport     C           2  7.862954240
38               Car     C           2 10.507826147
39               Car     D           2 26.767066484
40               Car     B           3  5.211386752
41               Car     C           3  2.547239953
42              Bike     C           4 10.893660760
43              Bike     B           2  3.048932392
44  Public_Transport     D           4 19.312018348
45  Public_Transport     D           0  1.250998323
46               Car     B           4 23.468056341
47               Car     F           2  1.307201008
48  Public_Transport     C           1  9.061298305
49               Car     B           2 16.386605089
50  Public_Transport     F           5 14.198586260
51  Public_Transport     D           2  5.903930680
52               Car     D           5 10.788057048
53               Car     F           0  2.847771212
54  Public_Transport     D           1 15.982389033
55  Public_Transport     B           1 33.808323654
56  Public_Transport     C           2 20.621755818
57               Car     F           3  4.990251950
58  Public_Transport     B           4 14.176132792
59  Public_Transport     C           2  0.179795340
60               Car     B           1 11.144025391
61               Car     B           2  6.207213615
62               Car     C           3 10.930397624
63  Public_Transport     C           1  3.191945825
64               Car     A           1 24.116514756
65              Walk     C           1  9.085949403
66              Walk     A           3  3.123102145
67               Car     C           2  4.905990076
68               Car     F           1 11.141701792
69               Car     C           1  7.942769108
70  Public_Transport     C           4 25.155511402
71              Walk     D           2  6.966238022
72  Public_Transport     B           0 12.714376537
73  Public_Transport     A           4 10.094412043
74               Car     D           0  0.492263361
75  Public_Transport     B           2  3.412106307
76  Public_Transport     B           3  0.868697702
77  Public_Transport     C           4  5.920769674
78               Car     C           1  5.628014676
79  Public_Transport     D           4  3.345181326
80  Public_Transport     D           4 16.678423936
81  Public_Transport     B           1  3.234560808
82  Public_Transport     F           3 20.669727642
83               Car     C           1  5.861409699
84  Public_Transport     C           2  7.774037436
85  Public_Transport     D           2  4.237668986
86               Car     B           2 15.835740114
87              Bike     A           2 22.383022838
88              Bike     C           4 64.356934684
89  Public_Transport     C           2 58.376442213
90  Public_Transport     D           3  8.445475412
91               Car     D           5 29.818047602
92               Car     C           2 12.984430205
93               Car     B           1 19.110928737
94               Car     B           0 72.178583905
95  Public_Transport     C           3  7.568940726
96              Walk     D           0 24.049530272
97  Public_Transport     C           2 12.698371948
98               Car     F           3  8.964170181
99         Motorbike     D           2 17.777711578
100             Bike     F           3  8.675561884
head(data)
         Transport Score Brotherhood       Time
1 Public_Transport     F           3  0.6225265
2              Car     F           4 36.6575119
3 Public_Transport     C           3 11.5225040
4 Public_Transport     D           1 33.8285932
5             Bike     D           0  9.0607207
6 Public_Transport     F           4  4.8575799
attach(data)
detach(data)

From a saved R workspace

rm(list=ls())
load("Students.RData")
data
           Transport Score Brotherhood         Time
1   Public_Transport     F           3  0.622526462
2                Car     F           4 36.657511927
3   Public_Transport     C           3 11.522504011
4   Public_Transport     D           1 33.828593223
5               Bike     D           0  9.060720704
6   Public_Transport     F           4  4.857579940
7   Public_Transport     A           5 11.671969750
8   Public_Transport     C           2 18.826682988
9                Car     D           4  1.900738537
10  Public_Transport     A           4 12.803518513
11               Car     D           1  2.836075382
12  Public_Transport     D           2 20.945708765
13              Walk     F           2 11.724335026
14         Motorbike     C           2 21.810021646
15  Public_Transport     C           5 34.394449328
16               Car     C           2 41.642181424
17               Car     B           4  2.348245194
18               Car     D           2 16.857141836
19  Public_Transport     B           3  5.928522913
20               Car     C           3 28.115604846
21              Walk     D           2  1.306014591
22  Public_Transport     B           1 86.365817737
23  Public_Transport     B           1 50.441392478
24              Bike     B           2 30.058228249
25  Public_Transport     B           3 24.271936752
26               Car     C           1  4.105579573
27               Car     D           3 47.241244459
28              Bike     D           0  0.007837325
29               Car     D           2  2.906950905
30              Bike     C           1 11.342407078
31              Walk     C           3 23.560583102
32  Public_Transport     F           3 26.990008915
33  Public_Transport     D           2 42.449839111
34              Bike     C           3  7.878798416
35         Motorbike     A           4  7.898013091
36               Car     D           2 22.147315960
37  Public_Transport     C           2  7.862954240
38               Car     C           2 10.507826147
39               Car     D           2 26.767066484
40               Car     B           3  5.211386752
41               Car     C           3  2.547239953
42              Bike     C           4 10.893660760
43              Bike     B           2  3.048932392
44  Public_Transport     D           4 19.312018348
45  Public_Transport     D           0  1.250998323
46               Car     B           4 23.468056341
47               Car     F           2  1.307201008
48  Public_Transport     C           1  9.061298305
49               Car     B           2 16.386605089
50  Public_Transport     F           5 14.198586260
51  Public_Transport     D           2  5.903930680
52               Car     D           5 10.788057048
53               Car     F           0  2.847771212
54  Public_Transport     D           1 15.982389033
55  Public_Transport     B           1 33.808323654
56  Public_Transport     C           2 20.621755818
57               Car     F           3  4.990251950
58  Public_Transport     B           4 14.176132792
59  Public_Transport     C           2  0.179795340
60               Car     B           1 11.144025391
61               Car     B           2  6.207213615
62               Car     C           3 10.930397624
63  Public_Transport     C           1  3.191945825
64               Car     A           1 24.116514756
65              Walk     C           1  9.085949403
66              Walk     A           3  3.123102145
67               Car     C           2  4.905990076
68               Car     F           1 11.141701792
69               Car     C           1  7.942769108
70  Public_Transport     C           4 25.155511402
71              Walk     D           2  6.966238022
72  Public_Transport     B           0 12.714376537
73  Public_Transport     A           4 10.094412043
74               Car     D           0  0.492263361
75  Public_Transport     B           2  3.412106307
76  Public_Transport     B           3  0.868697702
77  Public_Transport     C           4  5.920769674
78               Car     C           1  5.628014676
79  Public_Transport     D           4  3.345181326
80  Public_Transport     D           4 16.678423936
81  Public_Transport     B           1  3.234560808
82  Public_Transport     F           3 20.669727642
83               Car     C           1  5.861409699
84  Public_Transport     C           2  7.774037436
85  Public_Transport     D           2  4.237668986
86               Car     B           2 15.835740114
87              Bike     A           2 22.383022838
88              Bike     C           4 64.356934684
89  Public_Transport     C           2 58.376442213
90  Public_Transport     D           3  8.445475412
91               Car     D           5 29.818047602
92               Car     C           2 12.984430205
93               Car     B           1 19.110928737
94               Car     B           0 72.178583905
95  Public_Transport     C           3  7.568940726
96              Walk     D           0 24.049530272
97  Public_Transport     C           2 12.698371948
98               Car     F           3  8.964170181
99         Motorbike     D           2 17.777711578
100             Bike     F           3  8.675561884
head(data)
         Transport Score Brotherhood       Time
1 Public_Transport     F           3  0.6225265
2              Car     F           4 36.6575119
3 Public_Transport     C           3 11.5225040
4 Public_Transport     D           1 33.8285932
5             Bike     D           0  9.0607207
6 Public_Transport     F           4  4.8575799
attach(data)
detach(data)
save(data,file="Students.RData")
save.image("Students.RData")

Write functions

square=function(x)
{
res=x^2
return(res)  
}
square(2)
[1] 4
square.root=function(x)
{
if (x>=0) 
  {
  res=x^{1/2}
  return(res)  
  }
else 
  {
  print(paste(x,'is a negative number'))
  }
}
square.root(2)
[1] 1.414214
square.root(-1)
[1] "-1 is a negative number"
Syracuse=function(x,n)
{
 x_seq=x  
 for (j in 1:n) 
 {
  if (x%%2==0) 
  {
    x=x/2
  }
  else 
  {
    x=3*x+1
  }
 x_seq=c(x_seq,x)
 }
 return(list(n,x_seq))
}

Syracuse(17,100)
[[1]]
[1] 100

[[2]]
  [1] 17 52 26 13 40 20 10  5 16  8  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1
 [26]  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4
 [51]  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2
 [76]  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1
[101]  4
Syracuse_rec=function(x,n)
{
  m=length(x)
  res=x
  if (n>0)
  {
  if (x[m]%%2==0) 
  {
    res=Syracuse_rec(c(x,x[m]/2),n-1)
  }
  else 
  {
    res=Syracuse_rec(c(x,3*x[m]+1),n-1)
  }
  }
    return(res)
  
}

Syracuse_rec(17,100)
  [1] 17 52 26 13 40 20 10  5 16  8  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1
 [26]  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4
 [51]  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2
 [76]  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1  4  2  1
[101]  4
Fibonacci_game=function(x,y)
{
  num=rep(NA,10)
  num[1:2]=c(x,y)
  for (j in 3:10) 
  {
    num[j]=sum(num[j-(1:2)])
  }
  ratio=num[10]/num[9]
  total=sum(num)
  estim=num[7]*11
  return(list('num'=num,'ratio'=ratio, 'total'=total,'estim'=estim ))
}
Fibonacci_game(7,3)
$num
 [1]   7   3  10  13  23  36  59  95 154 249

$ratio
[1] 1.616883

$total
[1] 649

$estim
[1] 649