Vectores
vector_1 <- c(1,2,3,4,5,6,7,8,9)
vector_1
## [1] 1 2 3 4 5 6 7 8 9
Matrices
Mi_matriz <- matrix(data = vector_1, ncol = 3, byrow = FALSE)
Mi_matriz
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
Ponerle nombre a las columnas
colnames(Mi_matriz) <- c("Altura","Peso","Talla")
rownames(Mi_matriz) <- c("Juan","Pedro","Mario")
Mi_matriz
## Altura Peso Talla
## Juan 1 4 7
## Pedro 2 5 8
## Mario 3 6 9
Importar datos de un archivo csv
library(readr)
datos_groceries <- read_csv("datos_groceries.csv")
## Rows: 43367 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): item
## dbl (1): id_compra
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# datos_groceries <- read_csv("https://ricardorpalma.github.io/R-Dataset/datos_groceries.csv")
Creamos nuestro vector de datos para el analisis de tickets. Estos datos podriamos traerlo de un archivo, ponerlos en una linea de comando de r o traerlos desde la web
datos_crudo <- c(1,1,1,2,2,3,3,"5W40","Filtro Aceite","Filtro Aire","Correa Distr","Bomba de Agua","Filtro Nafta","Limpia Inyectores")
datos_crudo
## [1] "1" "1" "1"
## [4] "2" "2" "3"
## [7] "3" "5W40" "Filtro Aceite"
## [10] "Filtro Aire" "Correa Distr" "Bomba de Agua"
## [13] "Filtro Nafta" "Limpia Inyectores"
Vamos a transformar nuestros datos crudos en una matriz
lista_corta <- matrix(datos_crudo,ncol=2, byrow=FALSE)
lista_corta
## [,1] [,2]
## [1,] "1" "5W40"
## [2,] "1" "Filtro Aceite"
## [3,] "1" "Filtro Aire"
## [4,] "2" "Correa Distr"
## [5,] "2" "Bomba de Agua"
## [6,] "3" "Filtro Nafta"
## [7,] "3" "Limpia Inyectores"
Ahora le colocamos nombre a las columnas
dimnames(lista_corta) <- list(salida = c(1,2,3,4,5,6,7) , producto = c("ticket","producto") )
lista_corta
## producto
## salida ticket producto
## 1 "1" "5W40"
## 2 "1" "Filtro Aceite"
## 3 "1" "Filtro Aire"
## 4 "2" "Correa Distr"
## 5 "2" "Bomba de Agua"
## 6 "3" "Filtro Nafta"
## 7 "3" "Limpia Inyectores"
Datos en forma de lista larga
datos_m <- c(1,1,1,0,0,0,0, 0,0,0,1,1,0,0, 0,0,0,0,0,1,1)
datos_m
## [1] 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1
Ahora lo transformamos en una matriz
lista_larga <- matrix(datos_m,nrow=3,byrow=TRUE)
lista_larga
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1 1 1 0 0 0 0
## [2,] 0 0 0 1 1 0 0
## [3,] 0 0 0 0 0 1 1
Colocamos nombres a las filas y columnas
dimnames(lista_larga) <- list(ticket = c(1,2,3) , producto = c("5W40","Filtro Aceite","Filtro Aire","Correa Distr","Bomba de Agua","Filtro Nafta","Limpia Inyectores"))
lista_larga
## producto
## ticket 5W40 Filtro Aceite Filtro Aire Correa Distr Bomba de Agua Filtro Nafta
## 1 1 1 1 0 0 0
## 2 0 0 0 1 1 0
## 3 0 0 0 0 0 1
## producto
## ticket Limpia Inyectores
## 1 0
## 2 0
## 3 1
Recordemos antes de utilizar cualquier paquete de r que se invoca con el comando library, el paquete debe ser instalado
library(arules)
## Loading required package: Matrix
##
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
##
## abbreviate, write
apriori(lista_larga)
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.8 0.1 1 none FALSE TRUE 5 0.1 1
## maxlen target ext
## 10 rules TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 0
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[7 item(s), 3 transaction(s)] done [0.00s].
## sorting and recoding items ... [7 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 done [0.00s].
## writing ... [13 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
## set of 13 rules
transacciones_1 <- apriori(lista_larga)
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.8 0.1 1 none FALSE TRUE 5 0.1 1
## maxlen target ext
## 10 rules TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 0
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[7 item(s), 3 transaction(s)] done [0.00s].
## sorting and recoding items ... [7 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 done [0.00s].
## writing ... [13 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
reglas_a <- inspect(transacciones_1)
## lhs rhs support confidence
## [1] {Correa Distr} => {Bomba de Agua} 0.3333333 1
## [2] {Bomba de Agua} => {Correa Distr} 0.3333333 1
## [3] {Filtro Nafta} => {Limpia Inyectores} 0.3333333 1
## [4] {Limpia Inyectores} => {Filtro Nafta} 0.3333333 1
## [5] {Filtro Aceite} => {Filtro Aire} 0.3333333 1
## [6] {Filtro Aire} => {Filtro Aceite} 0.3333333 1
## [7] {Filtro Aceite} => {5W40} 0.3333333 1
## [8] {5W40} => {Filtro Aceite} 0.3333333 1
## [9] {Filtro Aire} => {5W40} 0.3333333 1
## [10] {5W40} => {Filtro Aire} 0.3333333 1
## [11] {Filtro Aceite, Filtro Aire} => {5W40} 0.3333333 1
## [12] {5W40, Filtro Aceite} => {Filtro Aire} 0.3333333 1
## [13] {5W40, Filtro Aire} => {Filtro Aceite} 0.3333333 1
## coverage lift count
## [1] 0.3333333 3 1
## [2] 0.3333333 3 1
## [3] 0.3333333 3 1
## [4] 0.3333333 3 1
## [5] 0.3333333 3 1
## [6] 0.3333333 3 1
## [7] 0.3333333 3 1
## [8] 0.3333333 3 1
## [9] 0.3333333 3 1
## [10] 0.3333333 3 1
## [11] 0.3333333 3 1
## [12] 0.3333333 3 1
## [13] 0.3333333 3 1
library(arulesViz)
plot(transacciones_1, method = "graph")
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
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.