function c Combina valores en un vector o lista.
> x <- c(10.4, 5.6, 3.1, 6.4, 21.7) ## assign a vector of numbers to object x. > z <- c(1,7:9) ## rango de valores > a <- c("Hello! World!") ## assign a character string to object a
Vectores Aritméticos
Operadores aritmeticos: +, -, *, /, ^, log, exp, sin, cos, tan, sqrt, min, max, length, sum, mean, var, sd, order, sort, rank > x <- c(1,2,3) > y <- c(5, 6, 7) > x + y > y<- 5 > x + y
ejemplo de manejo de vectores numericos
> x<- c(1, 2, NA, 3, 4) ## NA is missing value > is.na(x) > length(x) > mean(x) ## the command won't run. The default parameter for mean doesn't allow missing value. > help(mean) > mean(x, na.rm=T) ## set the parameter so the mean function allows missing value > x<- c(10.4, 5.6, 3.1, 6.4, 21.7) > var(x) # varianza > sum( (x-mean(x))^2 ) / (length(x)-1)
Generando secuencias:
> x<-1:10 > x<-seq(-5, 5, by=.2) > x<-rep(1:3, times=5) > x<-rep(1:3, each=5)
Vectores de caracteres
Funcion paste. Concatena vectores despues de convertirlos a caracter.
> paste(1:12) # same as as.character(1:12) > paste("Today is", date()) > x<-paste("Affy", 1:5, sep="") > x<-paste(c("X", "Y"), 1:5, sep="_")
Vectores Logicos:
> x<- (-5)+(0:15) > x>0 > x>0 & x<=5
Seleccion de valores de un vector:
> x<- (-5)+(0:15) > x > x[3:6] > x[x>0 & x<=5] > x[-(3:6)] ## - eliminar elementos del rango indicado > x[-(x>0 & x<=5)] > x[1:3]<-100:102 ## modify the first three number > x[(x>0 & x<=5)]<-999 ## modify the data satisfying the selection