R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Ejercicios Reporte 1: Vectores, tipos de datos, matrices, dataframe, listas & paquetes

1. Use rep() y seq() para generar un vector con los siguientes elementos:

x1 <- seq(from=0,to=4,by=1)
x1
## [1] 0 1 2 3 4
x2 <- factor(rep(x1,each=5))
x2
##  [1] 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
## Levels: 0 1 2 3 4

2. Use rep() y seq() para crear un vector con los siguientes elementos:

x3 <- seq(from=5,to=1,by=-1)
x3
## [1] 5 4 3 2 1
x4 <- factor(rep(x3,4))
x4
##  [1] 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1
## Levels: 1 2 3 4 5

3. Introduzca en R una matriz A usando: - Los datos del ejercicio 2 - Arreglo de 4 filas X 3 columnas. - Llenando los datos por columna.

#Creación de matriz.

matrizA<- matrix(data=x4,nrow=4,ncol= 3,byrow=FALSE)
matrizA
##      [,1] [,2] [,3]
## [1,] "5"  "1"  "2" 
## [2,] "4"  "5"  "1" 
## [3,] "3"  "4"  "5" 
## [4,] "2"  "3"  "4"
##Poner nombre a filas y columnas.

rownames(matrizA) <- c("fila1", "fila2", "fila3", "fila4")
colnames(matrizA) <- c("col_A", "col_B", "col_C")

matrizA
##       col_A col_B col_C
## fila1 "5"   "1"   "2"  
## fila2 "4"   "5"   "1"  
## fila3 "3"   "4"   "5"  
## fila4 "2"   "3"   "4"

4. Acceda a algunos elementos de la matriz de la siguiente manera:

#A[1,1:3]

x5<-matrizA[1,1:3]
x5
## col_A col_B col_C 
##   "5"   "1"   "2"
print("x5 selecciona la primera fila de la colunma 1 a la 3.")
## [1] "x5 selecciona la primera fila de la colunma 1 a la 3."
##A[1:4,2]

x6<-matrizA[1:4,2]
x6
## fila1 fila2 fila3 fila4 
##   "1"   "5"   "4"   "3"
print("x6 selecciona los datos de la fila 1 a la fila 4 de la segunda columna de la matriz")
## [1] "x6 selecciona los datos de la fila 1 a la fila 4 de la segunda columna de la matriz"
###A[3,3]

x7<-matrizA[3,3]
x7
## [1] "5"
print("x7 devuelve el valor situado en la fila 3 y la columna 3")
## [1] "x7 devuelve el valor situado en la fila 3 y la columna 3"
####A[11]

x8<-matrizA[11]
x8
## [1] "5"
print("x8 devuelve la observación #11 realizando el conteo por columna")
## [1] "x8 devuelve la observación #11 realizando el conteo por columna"
#####A[20]

x9<-matrizA[20]
x9
## [1] NA
print("x9 no devuelve valor porque no hay observación #20")
## [1] "x9 no devuelve valor porque no hay observación #20"
######A[-3,]

x10<-matrizA[-3,]
x10
##       col_A col_B col_C
## fila1 "5"   "1"   "2"  
## fila2 "4"   "5"   "1"  
## fila4 "2"   "3"   "4"
print("x10 devuelve la matriz sin la fila 3")
## [1] "x10 devuelve la matriz sin la fila 3"
#######A[5,4]
x11<-matrizA[5,4] 
## Error in matrizA[5, 4]: subscript out of bounds
print("x11 no devuelve valor porque no hay columna 5")
## [1] "x11 no devuelve valor porque no hay columna 5"

5. Recopile la información de su grupo referente a - Edad - Empresa - Nombre - Profesión(no utilice tildes).

Nombre <- c('David', 'Stephanie', 'Jose')
Edad <- c(32, 28, 23)
Empresa <- c('INS', 'BAC', 'BCCR')
Profesion <- c('Ing','Econ','Econ')
Nombre
## [1] "David"     "Stephanie" "Jose"
Edad
## [1] 32 28 23
Empresa
## [1] "INS"  "BAC"  "BCCR"
Profesion
## [1] "Ing"  "Econ" "Econ"

6. Introduzca según el tipo de datos adecuado a base de código R en un DataFrame la información recopilada en el punto anterior.

Datos <- data.frame(Nombre, Edad, Profesion,Empresa)

7.Visualice los datos registrados.

Datos
##      Nombre Edad Profesion Empresa
## 1     David   32       Ing     INS
## 2 Stephanie   28      Econ     BAC
## 3      Jose   23      Econ    BCCR

8. Visualice la estructura de los datos recopilados.

str(Datos)
## 'data.frame':    3 obs. of  4 variables:
##  $ Nombre   : Factor w/ 3 levels "David","Jose",..: 1 3 2
##  $ Edad     : num  32 28 23
##  $ Profesion: Factor w/ 2 levels "Econ","Ing": 2 1 1
##  $ Empresa  : Factor w/ 3 levels "BAC","BCCR","INS": 3 1 2

9. Compile en una sola lista los objetos creados para resolver los ejercicios anteriores.

Lista <- list(vector1=x2, vector2=x4, A=matrizA, Elem1=x5, elem2=x6, elem3=x7, elem4=x8, elem5=x9, elem5=x10, Name=Nombre, Age=Edad, Cia=Empresa, Prof=Profesion, Dat=Datos)
Lista
## $vector1
##  [1] 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
## Levels: 0 1 2 3 4
## 
## $vector2
##  [1] 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1
## Levels: 1 2 3 4 5
## 
## $A
##       col_A col_B col_C
## fila1 "5"   "1"   "2"  
## fila2 "4"   "5"   "1"  
## fila3 "3"   "4"   "5"  
## fila4 "2"   "3"   "4"  
## 
## $Elem1
## col_A col_B col_C 
##   "5"   "1"   "2" 
## 
## $elem2
## fila1 fila2 fila3 fila4 
##   "1"   "5"   "4"   "3" 
## 
## $elem3
## [1] "5"
## 
## $elem4
## [1] "5"
## 
## $elem5
## [1] NA
## 
## $elem5
##       col_A col_B col_C
## fila1 "5"   "1"   "2"  
## fila2 "4"   "5"   "1"  
## fila4 "2"   "3"   "4"  
## 
## $Name
## [1] "David"     "Stephanie" "Jose"     
## 
## $Age
## [1] 32 28 23
## 
## $Cia
## [1] "INS"  "BAC"  "BCCR"
## 
## $Prof
## [1] "Ing"  "Econ" "Econ"
## 
## $Dat
##      Nombre Edad Profesion Empresa
## 1     David   32       Ing     INS
## 2 Stephanie   28      Econ     BAC
## 3      Jose   23      Econ    BCCR

10. Instale y cargue el paquete swirl.Indique su utilidad y además brinde una pequeña guía de uso del paquete.

#La libreria Swirl fue creada (en R) para aprender a usar R. Para iniciar se debe contar con lo siguiente: 
            ##1. Laptop con capacidad para correr el paquete.
            ##2. Descargar R y R Studio.
            ##3. Seguir los siguientes pasos en R Studio.

###Paso 1: instalar y cargar el paquete. 

install.packages("swirl")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(swirl)
## 
## | Hi! I see that you have some variables saved in your workspace. To keep
## | things running smoothly, I recommend you clean up before starting swirl.
## 
## | Type ls() to see a list of the variables in your workspace. Then, type
## | rm(list=ls()) to clear your workspace.
## 
## | Type swirl() when you are ready to begin.
####Paso 2: limpiar el ambiente del lugar de trabajo en R. 
ls()
##  [1] "Datos"     "Edad"      "Empresa"   "Lista"     "matrizA"  
##  [6] "Nombre"    "Profesion" "x1"        "x10"       "x2"       
## [11] "x3"        "x4"        "x5"        "x6"        "x7"       
## [16] "x8"        "x9"
"rm(list=ls())"
## [1] "rm(list=ls())"
#####Paso 3: inicia el ambiente swirl.

"swirl()"
## [1] "swirl()"