class (m)
class (m[3])
m[3]
n <- c ("a", NA, "b")
n
class (n)
class (n[2])
n[2]
is.na (m)
is.na (n)
m
m+1
TRUE  & TRUE
TRUE  & FALSE
FALSE & FALSE
c (TRUE, TRUE) & c (TRUE, FALSE)
TRUE | TRUE
TRUE | FALSE
FALSE | FALSE
c (TRUE, TRUE) | c (TRUE, FALSE)
NA & TRUE
NA & FALSE
NA | FALSE
NA | TRUE
!NA
NA | FALSE
NA | TRUE
!NA
0 | 0
0 | 1
1 | 1
0 & 0
0 & 1
1 & 1
help(max)
?max
help.seach(max)
help.search(max)
help.start(max)
example(mx)
example(max)
help.search("max")
help.start("max")
help.start()
help.search()
c (1,2,3)
c ("a","b","c")
c ("a","b","c", 1,2,3)
length (c (1,2,3))
length (c ("a","b","c"))
length (c ("a","b","c", 1,2,3))
is.vector (1)
class (1)
length (1)
is.vector ("adjgfagkjg")
class ("adjgfagkjg")
length ("adjgfagkjg")
1
c (1)
1 == c (1)
identical (1, c (1))
identical (4, 3+1)
a <- 4
a
b <- c (1,2,3)
b
c <- c (5,4,3,2,1)
c
d <- c ("a", "b", "c", "d", "e", "f")
d
length (a)
length (b)
length (c)
length (d)
a
a <- 7
a
a <- c ("a","b","c")
a
class (1:10)               #"integer"
class (c (1.0, 2.0, 3.0))  #"numeric"
class (c ("A", "B", "C"))  #"character"
class (c (TRUE, FALSE))    #"logical"
d
d[1]
d[3]
d[c (1,2,3)]
d[c (3,2,1)]  #sigue siendo un vector en otro orden
v <- c (3,2,1)
class (v)  ## v es un vector
d[v]       ## que 'indexa' a otro vector d
class (d[c (1,2,3)])  # es un vector nuevo
length (d[c (1,2,3)]) # con longitud diferente
d
d[-1]
d[c (-1,-3,-5)]
d[ - c (1,3,5)]  # el operador - se aplica a todos los elementos del vector
10:15
c (10, 11, 12 ,13 ,14, 15)
identical (10:15, c(10, 11, 12 ,13 ,14, 15))
class (10:15)
class (c(10, 11, 12 ,13 ,14, 15))
d[3:1]
v <- 101:103
v
names (v)
names (v) <- c ("n1", "n2", "n3")
v
names (v)
c (N1 = "primer valor", N2 = "segundo valor")
v[2]
v["n2"]
v[3:2]
v[c("n3", "n2")]
v
logico <- c(TRUE, FALSE, TRUE)
is.vector (logico)
class (logico)
v[logico]
c (1,1,2,3) + c (4,5,6,9)
A <- c (1,1,2,3)
B <- c (4,5,6,9)
A + B
A * B
B / A
A^2
A^3
A**2
3*A ## es tambien una operacion entre elementos de vectores; el vector c(3) se extiende automaticamente
1/A
1/B
A
c (2,3) * A
c (3,3) * c (1,2,3,4)
c (1,2) * c (1,2,3,4) #es lo mismo que:
c (1,2,1,2) * c (1,2,3,4)
c (1,2,1) * c (1,2,3,4)
m <- c (1:2, NA, 4:6)
m
class (m)
class (m[3])
m[3]
n <- c ("a", NA, "b")
n
class (n)
class (n[2])
is.na (m)
is.na (n)
m
m+1
paste ("texto primero", "texto segundo", sep = " ")
paste ("texto primero", "texto segundo", sep = "_")
paste ("texto primero", "texto segundo", sep = " Y ")
paste ( c("texto1", "texto2"), c("TEXTO1", "TEXTO2", "TEXTO3", "TEXTO4"), sep = " ")
vectorlogico <- is.na (m)
vectorlogico
class (vectorlogico)
length (vectorlogico)
vectorlogico[1:3]
!TRUE
!FALSE
!vectorlogico       ##afectan a todos los elementos del vector
vector1 <- 1:4
vector2 <- c (12,34,56,34)
vector1
vector2
cbind (vector1, vector2) #column bind
rbind (vector1, vector2) #row bind
vector3 <- 1:2
vector3
cbind (vector3, vector2) #column bind
rbind (vector3, vector2) #row bind
class (cbind (vector3, vector2)) #es una matriz
M <- matrix (c (vector1, vector2), nrow = 4)
M
class (M)
nrow (M) #numero de filas
ncol (M) #numero de columnas
dim (M)  #dimension
dim (vector1)
M
M*2
M+1
M
M+(0:1)  #reciclado del vector 0:1 por columnas
M
M+(0:1)  #reciclado del vector 0:1 por columnas
colnames (M) <- c ("v1", "v2")
M
nombres <- colnames (M)
nombres
class (nombres)
length (nombres)
rownames (M)
rn <- rownames (M) #es un elemento NULL
rn
length (rn)
class (rn)  #tiene clase propia diferente de la de vector
c ("row1", "row2", "row3", "row4")
paste ("row", 1:4, sep = "") #otra forma de escribir los nombres
rownames (M) <- c ("row1", "row2", "row3", "row4") #ASIGNANDO nombres a las filas
M
rownames (M)
colnames (M) <- c ("col1", "col2")
M
colnames (M) <- c ("col1", "col2", "col3")
M
M[2, 1] #fila 2, columna 1
M[ , 1] #toda la columna 1
M[2,  ] #toda la fila 2
class (M)
class (M[,1])  ##recuperamos los vectores (columna)
class (M[2,])  ##recuperamos los vectores (fila)
dim (M)         #la matriz tiene dimension
dim (M[,1])     #un vector no tiene dimension
dim (M[2,])
M
M[1:2,]
M[c(1,3),]
M[c(1,3), 1]
M[c(1,3), 2]
class (M[1:2,])
M[,c(2,1)] #reordenar la matriz. Cambiamos las columnas.
M[c(1,3), 2] #es un vector con nombres
M
M[1, 1] <- 1000
M
M[,2] <- M[,2] * 10
M
M
M[2,2]
M["row2","col2"]
M[,c("col2", "col1")]
M[c("row3", "row1"),]
M[c("row3", "row1"), 1] #se pueden combinar las dos notaciones
M[c("row3", "row1"), 1]
M[c("row3", "row1"), 1] <- 0
M[c("row3", "row1"), 1]
M
M[c(TRUE, FALSE, FALSE, TRUE), c(FALSE, TRUE)]
M[c(1, 4), c(FALSE, TRUE)]
M[c(1, 4), c(FALSE, TRUE)]
M[c(1, 4), c(FALSE, TRUE)] <- M[c(4, 1), c(FALSE, TRUE)]
M[c(1, 4), c(FALSE, TRUE)]
class (M)
class (M[1,])
class (M[,1])
class (M[1,1])
M
M <- cbind (M, c (-5,-10))
M
class (M)
class (M[1,])
class (M[,1])
class (M[1,1])
N <- M
N[1,1] <- 1000
N
M
M <- cbind (M, "hola")
M
class (M)
class (M[1,])
class (M[,1])
class (M[1,1])
colnames (M)  # "" indica un texto de longitud 0
colnames (M)[3] <- "COL3"
colnames (M)[4] <- "COL4"
N > 500  ##matriz lógica
N[N > 500] <- NA
N
unVector <- 1:10
unVector
otroVector <- LETTERS ##LETTERS es una variable definida internamente en R
otroVector
unaMatriz <- cbind (1:10, 10:1)
rownames (unaMatriz) <- LETTERS[1:10]  #con nombres en las filas
colnames (unaMatriz) <- letters[1:2]   #y en las columnas
unaMatriz
lista0 <- list (unVector, otroVector, unaMatriz)
class (lista0)
length (lista0)
class (lista0)
length (lista0)
lista0[1]
lista0[1:2]
lista0[c(1,3)]  #reordenaciones
lista0
class (lista0[1])
length (lista0[1])
lista0[[1]]
class (lista0[[1]])
length (lista0[[1]])
names (lista0)
names (lista0) <- c("uno", "dos", "tres")
names (lista0)
lista1 <- list (uno=unVector, dos=otroVector, tres=unaMatriz)
lista1
lista1["uno"]
lista1[c("uno", "dos")]
class  (lista1["uno"])  #corchete unico mantiene la estructura de lista
length (lista1["uno"])  #alrededor del objeto
class  (lista1[["uno"]])  #corchete doble extrae el objeto de la posicion
length (lista1[["uno"]])  #en su propio formato
lista1$uno
class  (lista1$uno)
length (lista1$uno)
vector <- c(TRUE, FALSE, FALSE, TRUE)
vector
lista <- list("n" = 1:4, "l" = letters[1:4])
lista
matriz <- matrix(c(22, 33), nrow = 4, ncol = 2, byrow = TRUE)
matriz
data.frame (vector, lista)   #Cada elemento de la lista es una columna del data frame
data1 <- data.frame(matriz) #Cada columna de la matriz es una columna del data frame
data1
rownames (matriz) <- LETTERS[1:4]
colnames (matriz) <- letters[1:2]
data2 <- data.frame(lista, matriz, vector)
data2
class(data2)
length(data2)
data1
colnames(data1)
data2
colnames(data2)
nc <- colnames(data2)
nc
class (nc)
length (nc)
colnames (data2) <- paste0 ("dato", 1:5)
colnames (data2)
data1
rownames(data1)
nr <- rownames(data1)
nr
class(nr)
length(nr)
matriz
data2
nr <- rownames(data2)
nr
class(nr)
rownames(data2) <- paste0("obs", 1:4)
data2
data1[,3] <- 44
data1
data2
data2[3,5] #fila 3, columna 1
class(data2[3,5])
length(data2[3,5])
data2[ ,1] #columna 1
class(data2[ ,1]) #recuperamos los vectores columna
length(data2[ ,1])
data2[1]
class(data2[1])
length(data2[1])
data2[1, ] #fila 1
class(data2[1, ])
length(data2[1, ])
data2
data2[1:2, c(3,5)]
class(data2[1:2, c(3,5)])
data2["obs3",]
data2[,c("dato2","dato5")]
data2["dato1"]
data2[1, c("dato2","dato5")]
data2$dato1
class(data2$dato1)
data2[1:2,]$dato1
class(data2[1:2,]$dato1)
nrow(data2) #numero de filas
ncol(data2) #numero de columnas
dim(data2) #dimension
attach (data2) #hace "visible" el data frame, de modo que se puede acceder directamente a las variables por su nombre
search()
dato3
detach (data2) #dejamos de hacer "visible" el data frame
data2
data2[,2]
class(data2[,2])
f <- factor(c(LETTERS[1:3], "A", "C"))
class(f)
f
f
names(f) <- letters[1:5]
f
names(f)
f[1] #por posicion
f[2]
f[3]
length(f)
f(1)
f[1]
f[2]
f1 <- factor(c(1:3,5:4))
f1
f2 <- factor(c(3:1, 4:5))
f2
f1 == f2
f1 != f2
q()
2 + 2
2 / 3
2 + 2
2 / 3
variableA <- 3
variableA
variableB <- 4
variableB
variableA * 2
paco <- 35
PACO <- 38
PACO
paco
3^5
4 > 3
4 < 3
4 <= 3
4 == 3
4 = 3
4 != 3
sqrt (3)
sqrt (variableA)
?sqrt
?sqrt
sqrt (3)
sqrt (variableA)
SQRT (variableA)   #R es _case sensitive_ no funciona en mayusculas
variableA
help(sqrt)
c (1,2,3)
c ("a","b","c")
c ("a","b","c", 1,2,3)
vector1 <- c(1,5,7)
?c
length(vector1)
length (c (1,2,3))
length (c ("a","b","c"))
length (c ("a","b","c", 1,2,3))
a <- 4
a
b <- c (1,2,3)
b
c <- c (5,4,3,2,1)
c
d <- c ("a", "b", "c", "d", "e", "f")
d
length (a)
length (b)
length (c)
length (d)
1:10
1:100
numeritos <- c(1:100)
numeritos
length(numeritos)
class(numeritos)
a <- 7
class(a)
a
class(c(1:2))
class(a)
class (1:10)               #"integer"
class (c (1.0, 2.0, 3.0))  #"numeric"
class(c(1:2))
class(c(1:2))
class(a)
a <- 5.5
class(a)
a <- 5
class(a)
class (c (1.0, 2.0, 3.0))  #"numeric"
class (c ("A", "B", "C"))  #"character"
class (c (TRUE, FALSE))    #"logical"
a <- 5
class(a)
a <- c(5)
class(a)
d
d
d[1]
d[3]
d[c (1,2,3)]
d
d[c(1,2)]
d[1,2]
v <- c (3,2,1)
class (v)  ## v es un vector
d
d[v]       ## que 'indexa' a otro vector d
d
d[-1]
A <- c (1,1,2,3)
B <- c (4,5,6,9)
length(A)
length(B)
A + B
A * B
A
c (2,3) * A
c (1,2,1) * c (1,2,3,4)
variableA <- 3
variableA
variableB <- 4
variableB
?letters
class(letters)
letters
LETTERS
length(letters)
letters[c(1:5)]
