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:
Instalamos y cargamos los paquetes data.table y tidytable
options(repos = c(CRAN = "https://cran.rstudio.com/"))
install.packages("data.table")
## Installing package into 'C:/Users/Elisa/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'data.table' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'data.table'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problema al copiar
## C:\Users\Elisa\AppData\Local\R\win-library\4.3\00LOCK\data.table\libs\x64\data_table.dll
## a
## C:\Users\Elisa\AppData\Local\R\win-library\4.3\data.table\libs\x64\data_table.dll:
## Permission denied
## Warning: restored 'data.table'
##
## The downloaded binary packages are in
## C:\Users\Elisa\AppData\Local\Temp\RtmpG2Tqbi\downloaded_packages
install.packages("tidytable")
## Installing package into 'C:/Users/Elisa/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'tidytable' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Elisa\AppData\Local\Temp\RtmpG2Tqbi\downloaded_packages
library(data.table)
library(tidytable)
##
## Attaching package: 'tidytable'
## The following objects are masked from 'package:data.table':
##
## %notin%, between, first, fread, last
## The following objects are masked from 'package:stats':
##
## dt, filter, lag
## The following object is masked from 'package:base':
##
## %in%
lego_population_cof<-read.csv("lego_population_cof.csv")
summary(lego_population_cof)
## X Item_Number Set_Name Amazon_Price
## Min. : 1.0 Length:2307 Length:2307 Length:2307
## 1st Qu.: 577.5 Class :character Class :character Class :character
## Median :1154.0 Mode :character Mode :character Mode :character
## Mean :1154.0
## 3rd Qu.:1730.5
## Max. :2307.0
##
## Year Pages unique_pieces Theme
## Min. :2018 Mode:logical Min. : 1.0 Length:2307
## 1st Qu.:2018 NA's:2307 1st Qu.: 39.0 Class :character
## Median :2019 Median : 83.0 Mode :character
## Mean :2019 Mean : 118.6
## 3rd Qu.:2020 3rd Qu.: 164.0
## Max. :2020 Max. :1067.0
## NA's :974 NA's :1008
## Pieces Price Ages Minifigures
## Min. : 1 Mode:logical Length:2307 Min. : 1.000
## 1st Qu.: 70 NA's:2307 Class :character 1st Qu.: 1.000
## Median : 193 Mode :character Median : 3.000
## Mean : 397 Mean : 3.202
## 3rd Qu.: 456 3rd Qu.: 4.000
## Max. :9036 Max. :28.000
## NA's :986 NA's :1398
## Packaging Weight Availability Size
## Length:2307 Length:2307 Length:2307 Length:2307
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
head(lego_population_cof)
## X Item_Number Set_Name Amazon_Price Year Pages unique_pieces Theme Pieces
## 1 1 - <NA> NA NA NA <NA> NA
## 2 2 - <NA> NA NA NA <NA> NA
## 3 3 - <NA> - NA NA NA <NA> NA
## 4 4 - <NA> - NA NA NA <NA> NA
## 5 5 - <NA> €50.90 NA NA NA <NA> NA
## 6 6 - <NA> €45.04 NA NA NA <NA> NA
## Price Ages Minifigures Packaging Weight Availability Size
## 1 NA Ages_NA NA <NA> <NA> <NA> <NA>
## 2 NA Ages_NA NA <NA> <NA> <NA> <NA>
## 3 NA Ages_NA NA <NA> <NA> <NA> <NA>
## 4 NA Ages_NA NA <NA> <NA> <NA> <NA>
## 5 NA Ages_NA NA <NA> <NA> <NA> <NA>
## 6 NA Ages_NA NA <NA> <NA> <NA> <NA>
str(lego_population_cof)
## 'data.frame': 2307 obs. of 16 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Item_Number : chr "-" "-" "-" "-" ...
## $ Set_Name : chr NA NA NA NA ...
## $ Amazon_Price : chr "" "" "-" "-" ...
## $ Year : int NA NA NA NA NA NA NA NA NA NA ...
## $ Pages : logi NA NA NA NA NA NA ...
## $ unique_pieces: int NA NA NA NA NA NA NA NA NA NA ...
## $ Theme : chr NA NA NA NA ...
## $ Pieces : int NA NA NA NA NA NA NA NA NA NA ...
## $ Price : logi NA NA NA NA NA NA ...
## $ Ages : chr "Ages_NA" "Ages_NA" "Ages_NA" "Ages_NA" ...
## $ Minifigures : int NA NA NA NA NA NA NA NA NA NA ...
## $ Packaging : chr NA NA NA NA ...
## $ Weight : chr NA NA NA NA ...
## $ Availability : chr NA NA NA NA ...
## $ Size : chr NA NA NA NA ...
Convertimos lego_population_cof en un data.table con as.data.table()
legoDT <- as.data.table(lego_population_cof)
class(legoDT)
## [1] "data.table" "data.frame"
Vamos a quitar los NAs de Set_Name para quedarnos con los sets de Lego que tienen nombre
legoDT<-legoDT[!is.na(Set_Name)]
summary(legoDT)
## X Item_Number Set_Name Amazon_Price
## Min. : 731 Length:1333 Length:1333 Length:1333
## 1st Qu.:1120 Class :character Class :character Class :character
## Median :1460 Mode :character Mode :character Mode :character
## Mean :1501
## 3rd Qu.:1889
## Max. :2307
##
## Year Pages unique_pieces Theme
## Min. :2018 Mode:logical Min. : 1.0 Length:1333
## 1st Qu.:2018 NA's:1333 1st Qu.: 39.0 Class :character
## Median :2019 Median : 83.0 Mode :character
## Mean :2019 Mean : 118.6
## 3rd Qu.:2020 3rd Qu.: 164.0
## Max. :2020 Max. :1067.0
## NA's :34
## Pieces Price Ages Minifigures
## Min. : 1 Mode:logical Length:1333 Min. : 1.000
## 1st Qu.: 70 NA's:1333 Class :character 1st Qu.: 1.000
## Median : 193 Mode :character Median : 3.000
## Mean : 397 Mean : 3.202
## 3rd Qu.: 456 3rd Qu.: 4.000
## Max. :9036 Max. :28.000
## NA's :12 NA's :424
## Packaging Weight Availability Size
## Length:1333 Length:1333 Length:1333 Length:1333
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
head(legoDT)
## X Item_Number Set_Name Amazon_Price Year Pages
## <int> <char> <char> <char> <int> <lgcl>
## 1: 731 10260 Downtown Diner <NA> 2018 NA
## 2: 732 10261 Roller Coaster <NA> 2018 NA
## 3: 733 10262 James Bond Aston Martin DB5 <NA> 2018 NA
## 4: 734 10263 Winter Village Fire Station <NA> 2018 NA
## 5: 735 10264 Corner Garage <NA> 2019 NA
## 6: 736 10265 Ford Mustang <NA> 2019 NA
## unique_pieces Theme Pieces Price Ages Minifigures Packaging
## <int> <char> <int> <lgcl> <char> <int> <char>
## 1: 507 Creator Expert 2480 NA Ages_16+ 6 Box
## 2: 556 Creator Expert 4124 NA Ages_16+ 11 Box
## 3: 311 Creator Expert 1290 NA Ages_NA NA Box
## 4: 323 Creator Expert 1166 NA Ages_12+ 8 Box
## 5: 564 Creator Expert 2569 NA Ages_16+ 6 Box
## 6: 359 Creator Expert 1471 NA Ages_16+ NA Box
## Weight Availability Size
## <char> <char> <char>
## 1: <NA> LEGO exclusive Small
## 2: 5.8Kg (12.78 lb) LEGO exclusive Small
## 3: <NA> LEGO exclusive Small
## 4: 1.37Kg (3.01 lb) LEGO exclusive Small
## 5: 3.04Kg (6.7 lb) LEGO exclusive Small
## 6: <NA> LEGO exclusive Small
Eliminamos la primera columna y las columna de precio y páginas (no hay precios ni páginas en ningún set con nombre) y convertimos el Item_Number como numérico
legoDT[,X:=NULL]
legoDT[,Amazon_Price:=NULL]
legoDT[,Pages:=NULL]
legoDT[,Price:=NULL]
legoDT[, Item_Number := as.numeric(Item_Number)]
summary(legoDT)
## Item_Number Set_Name Year unique_pieces
## Min. : 10260 Length:1333 Min. :2018 Min. : 1.0
## 1st Qu.: 40197 Class :character 1st Qu.:2018 1st Qu.: 39.0
## Median : 41635 Mode :character Median :2019 Median : 83.0
## Mean : 87430 Mean :2019 Mean : 118.6
## 3rd Qu.: 71377 3rd Qu.:2020 3rd Qu.: 164.0
## Max. :4002020 Max. :2020 Max. :1067.0
## NA's :34
## Theme Pieces Ages Minifigures
## Length:1333 Min. : 1 Length:1333 Min. : 1.000
## Class :character 1st Qu.: 70 Class :character 1st Qu.: 1.000
## Mode :character Median : 193 Mode :character Median : 3.000
## Mean : 397 Mean : 3.202
## 3rd Qu.: 456 3rd Qu.: 4.000
## Max. :9036 Max. :28.000
## NA's :12 NA's :424
## Packaging Weight Availability Size
## Length:1333 Length:1333 Length:1333 Length:1333
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
legoDT<-legoDT
legoDT
## Item_Number Set_Name Year unique_pieces
## <num> <char> <int> <int>
## 1: 10260 Downtown Diner 2018 507
## 2: 10261 Roller Coaster 2018 556
## 3: 10262 James Bond Aston Martin DB5 2018 311
## 4: 10263 Winter Village Fire Station 2018 323
## 5: 10264 Corner Garage 2019 564
## ---
## 1329: 88012 Technic Hub 2020 1
## 1330: 88013 Technic Large Motor 2020 1
## 1331: 88014 Technic XL Motor 2020 1
## 1332: 92176 NASA Apollo Saturn V 2020 221
## 1333: 92177 Ship in a Bottle 2020 125
## Theme Pieces Ages Minifigures Packaging Weight
## <char> <int> <char> <int> <char> <char>
## 1: Creator Expert 2480 Ages_16+ 6 Box <NA>
## 2: Creator Expert 4124 Ages_16+ 11 Box 5.8Kg (12.78 lb)
## 3: Creator Expert 1290 Ages_NA NA Box <NA>
## 4: Creator Expert 1166 Ages_12+ 8 Box 1.37Kg (3.01 lb)
## 5: Creator Expert 2569 Ages_16+ 6 Box 3.04Kg (6.7 lb)
## ---
## 1329: Powered Up 1 Ages_NA NA <NA> <NA>
## 1330: Powered Up 1 Ages_NA NA <NA> <NA>
## 1331: Powered Up 1 Ages_NA NA <NA> <NA>
## 1332: Ideas 1969 Ages_14+ NA Box <NA>
## 1333: Ideas 962 Ages_12+ NA Box <NA>
## Availability Size
## <char> <char>
## 1: LEGO exclusive Small
## 2: LEGO exclusive Small
## 3: LEGO exclusive Small
## 4: LEGO exclusive Small
## 5: LEGO exclusive Small
## ---
## 1329: <NA> Small
## 1330: <NA> Small
## 1331: <NA> Small
## 1332: Retail - limited Small
## 1333: Retail - limited Small
Podemos seleccionar los sets de Lego lanzados en un determinado año, por ejemplo 2018
legoDT[ Year == "2018"]
## Item_Number Set_Name Year unique_pieces
## <num> <char> <int> <int>
## 1: 10260 Downtown Diner 2018 507
## 2: 10261 Roller Coaster 2018 556
## 3: 10262 James Bond Aston Martin DB5 2018 311
## 4: 10263 Winter Village Fire Station 2018 323
## 5: 10268 Vestas Wind Turbine 2018 229
## ---
## 444: 76111 Batman: Brother Eye Takedown 2018 103
## 445: 76112 App-Controlled Batmobile 2018 114
## 446: 88005 Headlight Set 2018 1
## 447: 88009 Hub 2018 NA
## 448: 88010 Remote Control 2018 1
## Theme Pieces Ages Minifigures Packaging
## <char> <int> <char> <int> <char>
## 1: Creator Expert 2480 Ages_16+ 6 Box
## 2: Creator Expert 4124 Ages_16+ 11 Box
## 3: Creator Expert 1290 Ages_NA NA Box
## 4: Creator Expert 1166 Ages_12+ 8 Box
## 5: Creator Expert 826 Ages_12+ 3 Box
## ---
## 444: DC Comics Super Heroes 269 Ages_6 - 12 3 Box
## 445: DC Comics Super Heroes 321 Ages_8+ 1 Box
## 446: Powered Up 1 Ages_7+ NA Polybag
## 447: Powered Up 1 Ages_NA NA <NA>
## 448: Powered Up 1 Ages_NA NA <NA>
## Weight Availability Size
## <char> <char> <char>
## 1: <NA> LEGO exclusive Small
## 2: 5.8Kg (12.78 lb) LEGO exclusive Small
## 3: <NA> LEGO exclusive Small
## 4: 1.37Kg (3.01 lb) LEGO exclusive Small
## 5: 2.16Kg (4.75 lb) LEGO exclusive Small
## ---
## 444: 0.46Kg (1.01 lb) Retail Small
## 445: 0.82Kg (1.81 lb) Retail Small
## 446: 0.01Kg (0.03 lb) LEGO exclusive Small
## 447: <NA> <NA> Small
## 448: <NA> <NA> Small
Sumamos el número total de piezas de los sets, sin considerar los NA
legoDT[ , .(SumePieces=sum(as.numeric(Pieces), na.rm = TRUE)) ]
## SumePieces
## <num>
## 1: 524492
Podemos obtener el número máximo de piezas, el promedio de piezas por set y el número máximo de piezas únicas
legoDT[, .(MaxPieces = max(as.numeric(Pieces), na.rm = TRUE), PiecesAve = mean(as.numeric(Pieces), na.rm = TRUE), MaxUniquePieces = max(as.numeric(unique_pieces), na.rm = TRUE)) ]
## MaxPieces PiecesAve MaxUniquePieces
## <num> <num> <num>
## 1: 9036 397.0416 1067
También podemos obtener métricas por año, por tema, por edad o por packaging. Por ejemplo, número medio de piezas por año:
legoDT[ , .(AvPiecesYear = mean(as.numeric(Pieces),na.rm=TRUE)), by = Year ]
## Year AvPiecesYear
## <int> <num>
## 1: 2018 352.9050
## 2: 2019 393.0565
## 3: 2020 443.7423
Número de sets por año:
legoDT[ , .(NumSets=.N),by=Year]
## Year NumSets
## <int> <int>
## 1: 2018 448
## 2: 2019 427
## 3: 2020 458
Gráfica del número de sets por año:
legoDT[ , .(NumSets=.N),by=Year] [plot(NumSets)]
## Null data.table (0 rows and 0 cols)
Número de sets por tema:
legoDT[ , .(NumSets=.N),by=Theme]
## Theme NumSets
## <char> <int>
## 1: Creator Expert 13
## 2: Icons 5
## 3: Classic 25
## 4: Juniors 18
## 5: Disney 48
## 6: Duplo 71
## 7: Ninjago 90
## 8: Miscellaneous 27
## 9: Star Wars 123
## 10: Harry Potter 30
## 11: Jurassic World 20
## 12: City 120
## 13: Education 29
## 14: Architecture 13
## 15: Minecraft 28
## 16: Ideas 16
## 17: <NA> 3
## 18: The LEGO Movie 2 32
## 19: Monkie Kid 13
## 20: Speed Champions 18
## 21: Super Mario 20
## 22: Friends 112
## 23: Unikitty 9
## 24: Marvel Super Heroes 57
## 25: Hidden Side 23
## 26: The LEGO Batman Movie 7
## 27: Creator 48
## 28: Trolls World Tour 9
## 29: Dots 19
## 30: DC Comics Super Heroes 31
## 31: Art 4
## 32: Promotional 55
## 33: Seasonal 20
## 34: BrickHeadz 67
## 35: Xtra 9
## 36: Collectable Minifigures 11
## 37: Brick Sketches 4
## 38: Mindstorms 2
## 39: Elves 7
## 40: The Powerpuff Girls 2
## 41: Technic 42
## 42: The LEGO Ninjago Movie 1
## 43: Nexo Knights 6
## 44: Minions: The Rise of Gru 2
## 45: Stranger Things 1
## 46: Overwatch 9
## 47: FORMA 4
## 48: Powered Up 10
## Theme NumSets
Número de sets por edad:
legoDT[ , .(NumSets=.N),by=Ages]
## Ages NumSets
## <char> <int>
## 1: Ages_16+ 24
## 2: Ages_NA 358
## 3: Ages_12+ 14
## 4: Ages_18+ 23
## 5: Ages_5 - 99 6
## 6: Ages_4 - 99 5
## 7: Ages_4 - 7 11
## 8: Ages_4+ 53
## 9: Ages_1 - 3 2
## 10: Ages_2 - 5 6
## 11: Ages_2+ 26
## 12: Ages_1+ 6
## 13: Ages_5+ 80
## 14: Ages_6+ 128
## 15: Ages_6 - 14 8
## 16: Ages_7 - 14 17
## 17: Ages_8+ 111
## 18: Ages_7+ 135
## 19: Ages_10+ 95
## 20: Ages_5 - 12 37
## 21: Ages_6 - 12 60
## 22: Ages_6 - 10 2
## 23: Ages_7 - 12 24
## 24: Ages_5 - 10 2
## 25: Ages_8 - 12 7
## 26: Ages_9 - 14 6
## 27: Ages_9+ 51
## 28: Ages_9 - 12 1
## 29: Ages_8 - 14 19
## 30: Ages_11+ 5
## 31: Ages_3+ 1
## 32: Ages_3 - 6 1
## 33: Ages_2 - 6 1
## 34: Ages_4 - 6 3
## 35: Ages_14+ 5
## Ages NumSets
Este resultado es muy árido, así que quiero homogeneizar la columna de edades, creando una variable adicional llamada Edad Mínima.
library(tidyverse)
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
## v dplyr 1.1.4 v readr 2.1.5
## v forcats 1.0.0 v stringr 1.5.1
## v ggplot2 3.5.0 v tibble 3.2.1
## v lubridate 1.9.3 v tidyr 1.3.1
## v purrr 1.0.2
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::across() masks tidytable::across()
## x dplyr::add_count() masks tidytable::add_count()
## x dplyr::add_tally() masks tidytable::add_tally()
## x dplyr::anti_join() masks tidytable::anti_join()
## x dplyr::arrange() masks tidytable::arrange()
## x dplyr::between() masks tidytable::between(), data.table::between()
## x dplyr::bind_cols() masks tidytable::bind_cols()
## x dplyr::bind_rows() masks tidytable::bind_rows()
## x dplyr::c_across() masks tidytable::c_across()
## x dplyr::case_match() masks tidytable::case_match()
## x dplyr::case_when() masks tidytable::case_when()
## x dplyr::coalesce() masks tidytable::coalesce()
## x tidyr::complete() masks tidytable::complete()
## x dplyr::consecutive_id() masks tidytable::consecutive_id()
## x dplyr::count() masks tidytable::count()
## x dplyr::cross_join() masks tidytable::cross_join()
## x tidyr::crossing() masks tidytable::crossing()
## x dplyr::cume_dist() masks tidytable::cume_dist()
## x dplyr::cur_column() masks tidytable::cur_column()
## x dplyr::cur_data() masks tidytable::cur_data()
## x dplyr::cur_group_id() masks tidytable::cur_group_id()
## x dplyr::cur_group_rows() masks tidytable::cur_group_rows()
## x dplyr::dense_rank() masks tidytable::dense_rank()
## x dplyr::desc() masks tidytable::desc()
## x dplyr::distinct() masks tidytable::distinct()
## x tidyr::drop_na() masks tidytable::drop_na()
## x tibble::enframe() masks tidytable::enframe()
## x tidyr::expand() masks tidytable::expand()
## x tidyr::expand_grid() masks tidytable::expand_grid()
## x tidyr::extract() masks tidytable::extract()
## x tidyr::fill() masks tidytable::fill()
## x dplyr::filter() masks tidytable::filter(), stats::filter()
## x dplyr::first() masks tidytable::first(), data.table::first()
## x dplyr::full_join() masks tidytable::full_join()
## x dplyr::group_by() masks tidytable::group_by()
## x dplyr::group_cols() masks tidytable::group_cols()
## x dplyr::group_split() masks tidytable::group_split()
## x dplyr::group_vars() masks tidytable::group_vars()
## x lubridate::hour() masks data.table::hour()
## x dplyr::if_all() masks tidytable::if_all()
## x dplyr::if_any() masks tidytable::if_any()
## x dplyr::if_else() masks tidytable::if_else()
## x dplyr::inner_join() masks tidytable::inner_join()
## x dplyr::is_grouped_df() masks tidytable::is_grouped_df()
## x lubridate::isoweek() masks data.table::isoweek()
## x dplyr::lag() masks tidytable::lag(), stats::lag()
## x dplyr::last() masks tidytable::last(), data.table::last()
## x dplyr::lead() masks tidytable::lead()
## x dplyr::left_join() masks tidytable::left_join()
## x purrr::map() masks tidytable::map()
## x purrr::map_chr() masks tidytable::map_chr()
## x purrr::map_dbl() masks tidytable::map_dbl()
## x purrr::map_df() masks tidytable::map_df()
## x purrr::map_dfc() masks tidytable::map_dfc()
## x purrr::map_dfr() masks tidytable::map_dfr()
## x purrr::map_int() masks tidytable::map_int()
## x purrr::map_lgl() masks tidytable::map_lgl()
## x purrr::map_vec() masks tidytable::map_vec()
## x purrr::map2() masks tidytable::map2()
## x purrr::map2_chr() masks tidytable::map2_chr()
## x purrr::map2_dbl() masks tidytable::map2_dbl()
## x purrr::map2_df() masks tidytable::map2_df()
## x purrr::map2_dfc() masks tidytable::map2_dfc()
## x purrr::map2_dfr() masks tidytable::map2_dfr()
## x purrr::map2_int() masks tidytable::map2_int()
## x purrr::map2_lgl() masks tidytable::map2_lgl()
## x purrr::map2_vec() masks tidytable::map2_vec()
## x lubridate::mday() masks data.table::mday()
## x dplyr::min_rank() masks tidytable::min_rank()
## x lubridate::minute() masks data.table::minute()
## x lubridate::month() masks data.table::month()
## x dplyr::mutate() masks tidytable::mutate()
## x dplyr::n() masks tidytable::n()
## x dplyr::n_distinct() masks tidytable::n_distinct()
## x dplyr::na_if() masks tidytable::na_if()
## x tidyr::nest() masks tidytable::nest()
## x dplyr::nest_by() masks tidytable::nest_by()
## x dplyr::nest_join() masks tidytable::nest_join()
## x tidyr::nesting() masks tidytable::nesting()
## x dplyr::nth() masks tidytable::nth()
## x dplyr::pick() masks tidytable::pick()
## x tidyr::pivot_longer() masks tidytable::pivot_longer()
## x tidyr::pivot_wider() masks tidytable::pivot_wider()
## x purrr::pmap() masks tidytable::pmap()
## x purrr::pmap_chr() masks tidytable::pmap_chr()
## x purrr::pmap_dbl() masks tidytable::pmap_dbl()
## x purrr::pmap_df() masks tidytable::pmap_df()
## x purrr::pmap_dfc() masks tidytable::pmap_dfc()
## x purrr::pmap_dfr() masks tidytable::pmap_dfr()
## x purrr::pmap_int() masks tidytable::pmap_int()
## x purrr::pmap_lgl() masks tidytable::pmap_lgl()
## x purrr::pmap_vec() masks tidytable::pmap_vec()
## x dplyr::pull() masks tidytable::pull()
## x lubridate::quarter() masks data.table::quarter()
## x dplyr::recode() masks tidytable::recode()
## x dplyr::reframe() masks tidytable::reframe()
## x dplyr::relocate() masks tidytable::relocate()
## x dplyr::rename() masks tidytable::rename()
## x dplyr::rename_with() masks tidytable::rename_with()
## x tidyr::replace_na() masks tidytable::replace_na()
## x dplyr::right_join() masks tidytable::right_join()
## x dplyr::row_number() masks tidytable::row_number()
## x dplyr::rowwise() masks tidytable::rowwise()
## x lubridate::second() masks data.table::second()
## x dplyr::select() masks tidytable::select()
## x dplyr::semi_join() masks tidytable::semi_join()
## x tidyr::separate() masks tidytable::separate()
## x tidyr::separate_longer_delim() masks tidytable::separate_longer_delim()
## x tidyr::separate_rows() masks tidytable::separate_rows()
## x tidyr::separate_wider_delim() masks tidytable::separate_wider_delim()
## x tidyr::separate_wider_regex() masks tidytable::separate_wider_regex()
## x dplyr::slice() masks tidytable::slice()
## x dplyr::slice_head() masks tidytable::slice_head()
## x dplyr::slice_max() masks tidytable::slice_max()
## x dplyr::slice_min() masks tidytable::slice_min()
## x dplyr::slice_sample() masks tidytable::slice_sample()
## x dplyr::slice_tail() masks tidytable::slice_tail()
## x dplyr::summarise() masks tidytable::summarise()
## x dplyr::summarize() masks tidytable::summarize()
## x dplyr::tally() masks tidytable::tally()
## x dplyr::top_n() masks tidytable::top_n()
## x dplyr::transmute() masks tidytable::transmute()
## x purrr::transpose() masks data.table::transpose()
## x dplyr::tribble() masks tidyr::tribble(), tibble::tribble(), tidytable::tribble()
## x tidyr::uncount() masks tidytable::uncount()
## x dplyr::ungroup() masks tidytable::ungroup()
## x tidyr::unite() masks tidytable::unite()
## x tidyr::unnest() masks tidytable::unnest()
## x tidyr::unnest_longer() masks tidytable::unnest_longer()
## x tidyr::unnest_wider() masks tidytable::unnest_wider()
## x purrr::walk() masks tidytable::walk()
## x lubridate::wday() masks data.table::wday()
## x lubridate::week() masks data.table::week()
## x lubridate::yday() masks data.table::yday()
## x lubridate::year() masks data.table::year()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
legoDT <- legoDT %>%
mutate(Edad_minima = as.numeric(str_extract(Ages, "(?<=_)[0-9]+")))
legoDT
## Item_Number Set_Name Year unique_pieces
## <num> <char> <int> <int>
## 1: 10260 Downtown Diner 2018 507
## 2: 10261 Roller Coaster 2018 556
## 3: 10262 James Bond Aston Martin DB5 2018 311
## 4: 10263 Winter Village Fire Station 2018 323
## 5: 10264 Corner Garage 2019 564
## ---
## 1329: 88012 Technic Hub 2020 1
## 1330: 88013 Technic Large Motor 2020 1
## 1331: 88014 Technic XL Motor 2020 1
## 1332: 92176 NASA Apollo Saturn V 2020 221
## 1333: 92177 Ship in a Bottle 2020 125
## Theme Pieces Ages Minifigures Packaging Weight
## <char> <int> <char> <int> <char> <char>
## 1: Creator Expert 2480 Ages_16+ 6 Box <NA>
## 2: Creator Expert 4124 Ages_16+ 11 Box 5.8Kg (12.78 lb)
## 3: Creator Expert 1290 Ages_NA NA Box <NA>
## 4: Creator Expert 1166 Ages_12+ 8 Box 1.37Kg (3.01 lb)
## 5: Creator Expert 2569 Ages_16+ 6 Box 3.04Kg (6.7 lb)
## ---
## 1329: Powered Up 1 Ages_NA NA <NA> <NA>
## 1330: Powered Up 1 Ages_NA NA <NA> <NA>
## 1331: Powered Up 1 Ages_NA NA <NA> <NA>
## 1332: Ideas 1969 Ages_14+ NA Box <NA>
## 1333: Ideas 962 Ages_12+ NA Box <NA>
## Availability Size Edad_minima
## <char> <char> <num>
## 1: LEGO exclusive Small 16
## 2: LEGO exclusive Small 16
## 3: LEGO exclusive Small NA
## 4: LEGO exclusive Small 12
## 5: LEGO exclusive Small 16
## ---
## 1329: <NA> Small NA
## 1330: <NA> Small NA
## 1331: <NA> Small NA
## 1332: Retail - limited Small 14
## 1333: Retail - limited Small 12
legoDT[ , .(NumSets=.N),by=Edad_minima]
## Edad_minima NumSets
## <num> <int>
## 1: 16 24
## 2: NA 358
## 3: 12 14
## 4: 18 23
## 5: 5 125
## 6: 4 72
## 7: 1 8
## 8: 2 33
## 9: 6 198
## 10: 7 176
## 11: 8 137
## 12: 10 95
## 13: 9 58
## 14: 11 5
## 15: 3 2
## 16: 14 5
Número de sets por tipo de packaging:
legoDT[ , .(NumSets=.N),by=Packaging]
## Packaging NumSets
## <char> <int>
## 1: Box 1051
## 2: Plastic box 13
## 3: Shrink-wrapped 4
## 4: Box with handle 1
## 5: <NA> 40
## 6: Tag 1
## 7: Tub 5
## 8: Box with backing card 5
## 9: Polybag 169
## 10: Blister pack 9
## 11: Other 4
## 12: Foil pack 22
## 13: Plastic canister 9
Quiero ver cuál es el set con más peso y cuál con menos peso. Para ello, extraigo los kilogramos de la variable Weight:
legoDT <- legoDT %>%
mutate(Peso = as.numeric(str_extract(Weight, "\\d+\\.?\\d*(?=Kg)")))
legoDT
## Item_Number Set_Name Year unique_pieces
## <num> <char> <int> <int>
## 1: 10260 Downtown Diner 2018 507
## 2: 10261 Roller Coaster 2018 556
## 3: 10262 James Bond Aston Martin DB5 2018 311
## 4: 10263 Winter Village Fire Station 2018 323
## 5: 10264 Corner Garage 2019 564
## ---
## 1329: 88012 Technic Hub 2020 1
## 1330: 88013 Technic Large Motor 2020 1
## 1331: 88014 Technic XL Motor 2020 1
## 1332: 92176 NASA Apollo Saturn V 2020 221
## 1333: 92177 Ship in a Bottle 2020 125
## Theme Pieces Ages Minifigures Packaging Weight
## <char> <int> <char> <int> <char> <char>
## 1: Creator Expert 2480 Ages_16+ 6 Box <NA>
## 2: Creator Expert 4124 Ages_16+ 11 Box 5.8Kg (12.78 lb)
## 3: Creator Expert 1290 Ages_NA NA Box <NA>
## 4: Creator Expert 1166 Ages_12+ 8 Box 1.37Kg (3.01 lb)
## 5: Creator Expert 2569 Ages_16+ 6 Box 3.04Kg (6.7 lb)
## ---
## 1329: Powered Up 1 Ages_NA NA <NA> <NA>
## 1330: Powered Up 1 Ages_NA NA <NA> <NA>
## 1331: Powered Up 1 Ages_NA NA <NA> <NA>
## 1332: Ideas 1969 Ages_14+ NA Box <NA>
## 1333: Ideas 962 Ages_12+ NA Box <NA>
## Availability Size Edad_minima Peso
## <char> <char> <num> <num>
## 1: LEGO exclusive Small 16 NA
## 2: LEGO exclusive Small 16 5.80
## 3: LEGO exclusive Small NA NA
## 4: LEGO exclusive Small 12 1.37
## 5: LEGO exclusive Small 16 3.04
## ---
## 1329: <NA> Small NA NA
## 1330: <NA> Small NA NA
## 1331: <NA> Small NA NA
## 1332: Retail - limited Small 14 NA
## 1333: Retail - limited Small 12 NA
summary(legoDT)
## Item_Number Set_Name Year unique_pieces
## Min. : 10260 Length:1333 Min. :2018 Min. : 1.0
## 1st Qu.: 40197 Class :character 1st Qu.:2018 1st Qu.: 39.0
## Median : 41635 Mode :character Median :2019 Median : 83.0
## Mean : 87430 Mean :2019 Mean : 118.6
## 3rd Qu.: 71377 3rd Qu.:2020 3rd Qu.: 164.0
## Max. :4002020 Max. :2020 Max. :1067.0
## NA's :34
## Theme Pieces Ages Minifigures
## Length:1333 Min. : 1 Length:1333 Min. : 1.000
## Class :character 1st Qu.: 70 Class :character 1st Qu.: 1.000
## Mode :character Median : 193 Mode :character Median : 3.000
## Mean : 397 Mean : 3.202
## 3rd Qu.: 456 3rd Qu.: 4.000
## Max. :9036 Max. :28.000
## NA's :12 NA's :424
## Packaging Weight Availability Size
## Length:1333 Length:1333 Length:1333 Length:1333
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
## Edad_minima Peso
## Min. : 1.000 Min. : 0.0000
## 1st Qu.: 6.000 1st Qu.: 0.1700
## Median : 7.000 Median : 0.3800
## Mean : 7.253 Mean : 0.6729
## 3rd Qu.: 8.000 3rd Qu.: 0.8150
## Max. :18.000 Max. :12.4200
## NA's :358 NA's :642
Y muestro cuáles son los sets que tienen pesan más. Muestro los 5 primeros:
set_mas_kg <- legoDT %>%
arrange(desc(Peso)) %>%
slice_head(n=5)
print(set_mas_kg)
## Item_Number Set_Name Year unique_pieces Theme Pieces
## <num> <char> <int> <int> <char> <int>
## 1: 75252 Imperial Star Destroyer 2019 450 Star Wars 4784
## 2: 42115 Lamborghini Sián FKP 37 2020 295 Technic 3696
## 3: 42082 Rough Terrain Crane 2018 262 Technic 4057
## 4: 21323 Grand Piano 2020 345 Ideas 3662
## 5: 42083 Bugatti Chiron 2018 308 Technic 3599
## Ages Minifigures Packaging Weight Availability Size
## <char> <int> <char> <char> <char> <char>
## 1: Ages_16+ 2 Box 12.42Kg (27.36 lb) LEGO exclusive Small
## 2: Ages_18+ NA Box 7.18Kg (15.81 lb) Retail - limited Small
## 3: Ages_NA NA Box 6.1Kg (13.44 lb) Retail Small
## 4: Ages_18+ NA Box 6.09Kg (13.41 lb) Retail - limited Small
## 5: Ages_16+ NA Box 5.92Kg (13.04 lb) Retail - limited Small
## Edad_minima Peso
## <num> <num>
## 1: 16 12.42
## 2: 18 7.18
## 3: NA 6.10
## 4: 18 6.09
## 5: 16 5.92
Ahora quiero ver si hay relación entre la edad mínima y el número de piezas. Grafico las dos variables y calculo el coeficiente de correlación.
ggplot(legoDT, aes(x = Edad_minima, y = Pieces)) +
geom_point() +
labs(x = "Edad mínima", y = "Piezas",
title = "Relación entre Edad mínima y Piezas")
## Warning: Removed 359 rows containing missing values or values outside the scale range
## (`geom_point()`).
corr <- cor(legoDT$Edad_minima, legoDT$Pieces,use="complete.obs")
print(corr)
## [1] 0.6628583
O si hay relación entre el peso del set y el número de piezas. De la misma manera, grafico las dos variables y calculo el coeficiente de correlación.
ggplot(legoDT, aes(x = Peso, y = Pieces)) +
geom_point() +
labs(x = "Peso", y = "Piezas",
title = "Relación entre Peso y Piezas")
## Warning: Removed 642 rows containing missing values or values outside the scale range
## (`geom_point()`).
corr <- cor(legoDT$Peso, legoDT$Pieces,use="complete.obs")
print(corr)
## [1] 0.8872942