USO DE MANUAL R

USO DE ALGUNAS FUNCIONES DEL LIBRO MANUAL R

url <- 'https://raw.githubusercontent.com/fhernanb/datos/master/medidas_cuerpo'
dt1 <- read.table(url, header=T)

head(dt1)
  edad peso altura   sexo muneca biceps
1   43 87.3  188.0 Hombre   12.2   35.8
2   65 80.0  174.0 Hombre   12.0   35.0
3   45 82.3  176.5 Hombre   11.2   38.5
4   37 73.6  180.3 Hombre   11.2   32.2
5   55 74.1  167.6 Hombre   11.8   32.9
6   33 85.9  188.0 Hombre   12.4   38.5
summary(dt1)
      edad            peso           altura          sexo          
 Min.   :19.00   Min.   :42.00   Min.   :147.2   Length:36         
 1st Qu.:24.75   1st Qu.:54.95   1st Qu.:164.8   Class :character  
 Median :28.00   Median :71.50   Median :172.7   Mode  :character  
 Mean   :31.44   Mean   :68.95   Mean   :171.6                     
 3rd Qu.:37.00   3rd Qu.:82.40   3rd Qu.:179.4                     
 Max.   :65.00   Max.   :98.20   Max.   :190.5                     
     muneca           biceps     
 Min.   : 8.300   Min.   :23.50  
 1st Qu.: 9.475   1st Qu.:25.98  
 Median :10.650   Median :32.15  
 Mean   :10.467   Mean   :31.17  
 3rd Qu.:11.500   3rd Qu.:35.05  
 Max.   :12.400   Max.   :40.40  
dt1_tall<-subset(dt1,altura>180,c(edad,peso,altura,sexo))
dt2 <- subset(x=dt1, subset=altura > 185 & peso > 80,
select=c('sexo', 'edad', 'peso', 'altura'))
dt2
     sexo edad peso altura
1  Hombre   43 87.3  188.0
6  Hombre   33 85.9  188.0
15 Hombre   30 98.2  190.5
dt1_tall
   edad peso altura   sexo
1    43 87.3  188.0 Hombre
4    37 73.6  180.3 Hombre
6    33 85.9  188.0 Hombre
7    25 73.2  180.3 Hombre
9    28 65.9  183.0 Hombre
10   26 90.9  183.0 Hombre
15   30 98.2  190.5 Hombre
17   35 83.2  180.3 Hombre
18   37 83.2  180.3 Hombre
set.seed(123)
mivector <- rnorm(n=5,mean=12,sd=2) #genera valores normales media 12 y desviacion 2
matriz2 <- matrix(data=1:12, ncol=6)
milista <- list(E1=mivector, E2=matriz2, E3=dt2)
milista
$E1
[1] 10.87905 11.53965 15.11742 12.14102 12.25858

$E2
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    5    7    9   11
[2,]    2    4    6    8   10   12

$E3
     sexo edad peso altura
1  Hombre   43 87.3  188.0
6  Hombre   33 85.9  188.0
15 Hombre   30 98.2  190.5
#como pedir datos a una persona
my_name <- readline(prompt="Ingrese su nombre: ")
Ingrese su nombre: 
my_age <- readline(prompt="Ingrese su edad en años: ")
Ingrese su edad en años: 
my_age <- as.integer(my_age) # convert character into integer
cat("Hola,", my_name,
"el año siguiente usted tendra",
my_age + 1,
"años de edad.")#tambien se puede con print(paste())
Hola,  el año siguiente usted tendra NA años de edad.
#install.packages("svDialogs") 
#library(svDialogs)
#se usa para cuadros de dialogo y no en consola
#my_name <- dlgInput(message="Ingrese su nombre: ")$res
#my_age <- dlgInput(message="Ingrese su edad en años: ")$res
#my_age <- as.integer(my_age) # convert character into integer
#print(paste("Hola,", my_name,
#"el año siguiente usted tendá",
#my_age + 1,
#"años de edad."))
edad <- c(15, 19, 13, NA, 20)
deporte <- c(TRUE, TRUE, NA, FALSE, TRUE)
comic_fav <- c(NA, 'Superman', 'Batman', NA, 'Batman')
mimarco<-data.frame(edad,deporte,comic_fav)
mimarco
  edad deporte comic_fav
1   15    TRUE      <NA>
2   19    TRUE  Superman
3   13      NA    Batman
4   NA   FALSE      <NA>
5   20    TRUE    Batman
#vamos a crear otro vector donde eliminamos los vacios
mimarco_clean<-na.omit(mimarco)
mimarco_clean
  edad deporte comic_fav
2   19    TRUE  Superman
5   20    TRUE    Batman
#uso de la funcion sapply
sapply(split(x=dt1$altura,f=dt1$sexo),mean)
  Hombre    Mujer 
179.0778 164.0333 
#se verifica que se hace una diferencia con el argumento split((vector,descripcion),que quiero)

tapply(X = dt1$altura, INDEX = dt1$sexo, FUN = mean)
  Hombre    Mujer 
179.0778 164.0333 
tapply(mimarco$edad, mimarco$comic_fav, mean, na.rm = TRUE)
  Batman Superman 
    16.5     19.0