Creacion de objetos

n=15
n
## [1] 15
5->n
n
## [1] 5
n=10+2
n
## [1] 12
n=3+rnorm(1)
n
## [1] 1.106024
(10+2)*5
## [1] 60

Case sensitive

x=1
x
## [1] 1
X=10
X
## [1] 10

Listado de variables

name="Carmen";n1=1;n2=100;m=0.5
ls()
## [1] "m"    "n"    "n1"   "n2"   "name" "x"    "X"
ls(pat="m")
## [1] "m"    "name"
ls(pat="^n")
## [1] "n"    "n1"   "n2"   "name"
ls.str()
## m :  num 0.5
## n :  num 1.11
## n1 :  num 1
## 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 1
##  $ n2: num 100
##  $ m : num 0.5

Borrado de variables

x=1
rm(x)
rm(list=ls(pat="^m"))
rm(list=ls())

Ayuda en linea

?lm #funciones
help("*") #simbolos
help("bs",try.all.packages = TRUE) #bases de datos
## 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

Objetos

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
exp(-x)
## [1] 0
x-x
## [1] NaN
cit="ella dijo: \"Las comillas se pueden inclur en textos en R.\""
cat(cit)
## ella dijo: "Las comillas se pueden inclur en textos en R."
N=2.1e23
N
## [1] 2.1e+23

Generacion de datos

x=1: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.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
z=scan()
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 = 20)
##  [1] 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

Matrices

matrix(data=5,nr=2,nc=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
dim(x)
## NULL
dim(x)=c(5,3)

Marco de Datos

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)
L1=list(x,y)
L2=list(A=x,B=y)
names(L1)
## NULL
names(L2)
## [1] "A" "B"

Serie 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

Expresiones

x=3;y=2.5;z=1
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)

Acceder a Objetos

x=1:10
x[x>=5]=20
x[x==1]=25
x=rpois(40, lambda=5)
x[x%%2==0]
##  [1] 8 4 6 4 6 4 4 6 2 6 6 6 8 4
x=c(1: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

Calculo de matrices

m1=matrix(1,2,2)
m2=matrix(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
diag(3) # matriz identidad
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
v=c(10,20,30)
diag(v)
##      [,1] [,2] [,3]
## [1,]   10    0    0
## [2,]    0   20    0
## [3,]    0    0   30
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

bucles

y=numeric(length(x))
b=5;for(i in 1:length(x)) if (x[i]==b) y[i]=0 else y[i]=1
z=numeric(length(x))
for(i in 1:length(x)) z[i]=x[i]+y[i]
y=numeric(length(x))
y[x==b]=0;y[x!=b]=1
z=numeric(length(x))
z=x+y

funciones

foo=function() print(x)
x=1
foo()
## [1] 1
foo2=function() {x=2; print(x)}
foo2()
## [1] 2