1. Creacion de Objetos en Memoria
n=15
n
## [1] 15
5->n
n
## [1] 5
x=1
X=10
x
## [1] 1
X
## [1] 10
n=10+2
n
## [1] 12
n=3+rnorm(1)
n
## [1] 2.186588
(10+2)*5
## [1] 60
  1. Listado de Objetos
name="Carmen"
n1=10
n2=100
m=0.5
ls()
## [1] "m"    "n"    "n1"   "n2"   "name" "x"    "X"
ls(pat="m") 
## [1] "m"    "name"
ls(pat="^m")
## [1] "m"
ls.str() # listado con tipo
## m :  num 0.5
## n :  num 2.19
## n1 :  num 10
## n2 :  num 100
## name :  chr "Carmen"
## x :  num 1
## X :  num 10
M=data.frame(n1,n2,m)
ls.str(pat="M")
## M : 'data.frame':    1 obs. of  3 variables:
##  $ n1: num 10
##  $ n2: num 100
##  $ m : num 0.5
#ls.str(pat="M", max.level= -1)
  1. Borrado de Objetos
rm(X)
rm(list=ls(pat="^m"))
rm(list=ls())
ls()
## character(0)
  1. Ayuda en linea
?lm
help("*")
help("bs", try.all.packages = TRUE)
## Help for topic 'bs' is not in any loaded package but can be found in
## the following packages:
## 
##   Package               Library
##   splines               /Library/Frameworks/R.framework/Versions/4.0/Resources/library
  1. objeto
x=1
mode(x)
## [1] "numeric"
length(x)
## [1] 1
A="Gomphotherium"
compar=TRUE
z=1i
mode(A);mode(compar);mode(z)
## [1] "character"
## [1] "logical"
## [1] "complex"
x=5/0
exp(x)
## [1] Inf
x
## [1] Inf
exp(-x)
## [1] 0
cit="Ella dijo:\"Las comillas se pueden incluir en textos en R.\""
cit
## [1] "Ella dijo:\"Las comillas se pueden incluir en textos en R.\""
cat(cit)
## Ella dijo:"Las comillas se pueden incluir en textos en R."
N=2.1e23
N
## [1] 2.1e+23
  1. Generacion de Datos regulares
x=1:30 # una secuencia de numeros espaciados en unidad
x
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30
1:10-1
##  [1] 0 1 2 3 4 5 6 7 8 9
1:(10-1)
## [1] 1 2 3 4 5 6 7 8 9
seq(1,5,0.5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
seq(length=9, from=1, to=5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
c(1,1.5,2,2.5,3,3.5,4,4.5,5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
z=scan()
z
## numeric(0)
rep(1,30)
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
gl(3,5)
##  [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
## Levels: 1 2 3
gl(3,5,length=30)
##  [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
## Levels: 1 2 3
gl(2,6, label=c("M","F"))
##  [1] M M M M M M F F F F F F
## Levels: M F
gl(2,10)
##  [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
## Levels: 1 2
gl(2,1, length = 30)
##  [1] 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
## Levels: 1 2
gl(2,2, length = 20)
##  [1] 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2
## Levels: 1 2
expand.grid(a=c(60,80),p=c(100,300),sexo=c("M","F"))
##    a   p sexo
## 1 60 100    M
## 2 80 100    M
## 3 60 300    M
## 4 80 300    M
## 5 60 100    F
## 6 80 100    F
## 7 60 300    F
## 8 80 300    F
  1. Datos Aleatorios
rnorm(2)
## [1] -0.9968559  0.3452928
rexp(2)
## [1] 0.07068876 1.39296315
  1. Matrices
matrix(data = 5, nr=2,nc=2)
##      [,1] [,2]
## [1,]    5    5
## [2,]    5    5
matrix(5,2,2)
##      [,1] [,2]
## [1,]    5    5
## [2,]    5    5
matrix(1:6,2,3)
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
matrix(1:6,2,3, byrow = TRUE)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
x=1:15
x
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
dim(x)
## NULL
dim(x)=c(5,3)
x
##      [,1] [,2] [,3]
## [1,]    1    6   11
## [2,]    2    7   12
## [3,]    3    8   13
## [4,]    4    9   14
## [5,]    5   10   15
  1. Data frame
x=1:4;n=10;M=c(10,35);y=2:4
data.frame(x,n)
##   x  n
## 1 1 10
## 2 2 10
## 3 3 10
## 4 4 10
data.frame(x,M)
##   x  M
## 1 1 10
## 2 2 35
## 3 3 10
## 4 4 35
#data.frame(x,y)
  1. Listas
L1=list(x,y)
L2=list(A=x,B=y)
L1
## [[1]]
## [1] 1 2 3 4
## 
## [[2]]
## [1] 2 3 4
L2
## $A
## [1] 1 2 3 4
## 
## $B
## [1] 2 3 4
names(L1)
## NULL
names(L2)
## [1] "A" "B"
  1. series de tiempo
ts(1:10, start = 1959)
## Time Series:
## Start = 1959 
## End = 1968 
## Frequency = 1 
##  [1]  1  2  3  4  5  6  7  8  9 10
ts(1:47, frequency = 12,start = c(1959,2))
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1959       1   2   3   4   5   6   7   8   9  10  11
## 1960  12  13  14  15  16  17  18  19  20  21  22  23
## 1961  24  25  26  27  28  29  30  31  32  33  34  35
## 1962  36  37  38  39  40  41  42  43  44  45  46  47
ts(1:10,frequency = 4, start=c(1959,2))
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1959         1    2    3
## 1960    4    5    6    7
## 1961    8    9   10
  1. Expresiones
x=3;y=2.5;z=1
exp1=expression(x/(y+exp(z)))
exp1
## expression(x/(y + exp(z)))
eval(exp1)
## [1] 0.5749019
D(exp1,"x")
## 1/(y + exp(z))
D(exp1,"y")
## -(x/(y + exp(z))^2)
D(exp1,"z")
## -(x * exp(z)/(y + exp(z))^2)
  1. como acceder a valores
x=1:10
x
##  [1]  1  2  3  4  5  6  7  8  9 10
x[x>=5]=20
x
##  [1]  1  2  3  4 20 20 20 20 20 20
x[x==1]=25
x
##  [1] 25  2  3  4 20 20 20 20 20 20
x=rpois(40, lambda = 5)
x
##  [1]  4  3  7  3  7  4  4  5  9  4  5  4  2  4  4  6  3  4  5  1  5  4  7  5  5
## [26]  4  4  5  3  8  4 10  6  4  7  3  4  2  5  4
x[x%%2==0]
##  [1]  4  4  4  4  4  2  4  4  6  4  4  4  4  8  4 10  6  4  4  2  4
x=1:40
x
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
s=c(FALSE,TRUE)
x[s]
##  [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
  1. Calculo con matrices
m1=matrix(1,2,2)
m1
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    1
m2=matrix(2,2,2)
m2
##      [,1] [,2]
## [1,]    2    2
## [2,]    2    2
rbind(m1,m2)
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    1
## [3,]    2    2
## [4,]    2    2
cbind(m1,m2)
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    2    2
## [2,]    1    1    2    2
rbind(m1,m2) %*% cbind(m1,m2)
##      [,1] [,2] [,3] [,4]
## [1,]    2    2    4    4
## [2,]    2    2    4    4
## [3,]    4    4    8    8
## [4,]    4    4    8    8
cbind(m1,m2) %*% rbind(m1,m2)
##      [,1] [,2]
## [1,]   10   10
## [2,]   10   10
diag(m1)
## [1] 1 1
diag(rbind(m1,m2) %*% cbind(m1,m2))
## [1] 2 2 8 8
diag(m1)=10
v=c(10,20,30)
diag(v)
##      [,1] [,2] [,3]
## [1,]   10    0    0
## [2,]    0   20    0
## [3,]    0    0   30
diag(3)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
diag(2.1,3,5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  2.1  0.0  0.0    0    0
## [2,]  0.0  2.1  0.0    0    0
## [3,]  0.0  0.0  2.1    0    0
  1. Bucles
x
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
y=numeric(length(x))
b=20
for(i in 1:length(x)) if (x[i]==b) {y[i]=0} else {y[i]=1}
y
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [39] 1 1
x=1:40
x
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
y[x==b]=0
y[x!=b]=1
y
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [39] 1 1
z=numeric(length(x))
for(i in 1:length(x)) z[i]=x[i]+y[i]
z
##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 20 22 23 24 25 26
## [26] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
z=x+y
z
##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 20 22 23 24 25 26
## [26] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  1. funciones
foo=function() print(x)
x=10
foo()
## [1] 10
foo2=function() {x=2; print(x)}
foo2()
## [1] 2