Library dplyr
Dataset
# Cargar los paquetes que se necesiten
## install.packages(dplyr)
## install.packages(tidyverse)
# Llamar las librerías
library(dplyr) #Librería para manipular datos
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr) #Para leer archivos .csv
billboard100 <- read_csv("billboard100.csv")
## Rows: 330087 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): song, artist
## dbl (4): rank, last-week, peak-rank, weeks-on-board
## date (1): date
##
## ℹ 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.
Piping %>%
## Muestra primeras filas de vector, lista, data frame o matriz
head(billboard100,10)
## # A tibble: 10 × 7
## date rank song artist `last-week` `peak-rank` `weeks-on-board`
## <date> <dbl> <chr> <chr> <dbl> <dbl> <dbl>
## 1 2021-11-06 1 Easy On Me Adele 1 1 3
## 2 2021-11-06 2 Stay The K… 2 1 16
## 3 2021-11-06 3 Industry Ba… Lil N… 3 1 14
## 4 2021-11-06 4 Fancy Like Walke… 4 3 19
## 5 2021-11-06 5 Bad Habits Ed Sh… 5 2 18
## 6 2021-11-06 6 Way 2 Sexy Drake… 6 1 8
## 7 2021-11-06 7 Shivers Ed Sh… 9 7 7
## 8 2021-11-06 8 Good 4 U Olivi… 7 1 24
## 9 2021-11-06 9 Need To Know Doja … 11 9 20
## 10 2021-11-06 10 Levitating Dua L… 8 2 56
## Argumento %>% o tubería encadena operaciones
billboard100 %>% head(10)
## # A tibble: 10 × 7
## date rank song artist `last-week` `peak-rank` `weeks-on-board`
## <date> <dbl> <chr> <chr> <dbl> <dbl> <dbl>
## 1 2021-11-06 1 Easy On Me Adele 1 1 3
## 2 2021-11-06 2 Stay The K… 2 1 16
## 3 2021-11-06 3 Industry Ba… Lil N… 3 1 14
## 4 2021-11-06 4 Fancy Like Walke… 4 3 19
## 5 2021-11-06 5 Bad Habits Ed Sh… 5 2 18
## 6 2021-11-06 6 Way 2 Sexy Drake… 6 1 8
## 7 2021-11-06 7 Shivers Ed Sh… 9 7 7
## 8 2021-11-06 8 Good 4 U Olivi… 7 1 24
## 9 2021-11-06 9 Need To Know Doja … 11 9 20
## 10 2021-11-06 10 Levitating Dua L… 8 2 56
## El punto al final limita que solo se muestren como en este caso 3 filas
3 %>% head(billboard100, .)
## # A tibble: 3 × 7
## date rank song artist `last-week` `peak-rank` `weeks-on-board`
## <date> <dbl> <chr> <chr> <dbl> <dbl> <dbl>
## 1 2021-11-06 1 Easy On Me Adele 1 1 3
## 2 2021-11-06 2 Stay The K… 2 1 16
## 3 2021-11-06 3 Industry Baby Lil N… 3 1 14
Select
## Selecciona columnas específicas (date, rank, song, artist, week-on-board)
## Se necesitan las comillas sencillas por lo que el nombre de la columna tiene guiones
billboard100 %>%
select(date, rank, song, artist,'weeks-on-board')
## # A tibble: 330,087 × 5
## date rank song artist `weeks-on-board`
## <date> <dbl> <chr> <chr> <dbl>
## 1 2021-11-06 1 Easy On Me Adele 3
## 2 2021-11-06 2 Stay The Kid LAROI & Justin Bieber 16
## 3 2021-11-06 3 Industry Baby Lil Nas X & Jack Harlow 14
## 4 2021-11-06 4 Fancy Like Walker Hayes 19
## 5 2021-11-06 5 Bad Habits Ed Sheeran 18
## 6 2021-11-06 6 Way 2 Sexy Drake Featuring Future & You… 8
## 7 2021-11-06 7 Shivers Ed Sheeran 7
## 8 2021-11-06 8 Good 4 U Olivia Rodrigo 24
## 9 2021-11-06 9 Need To Know Doja Cat 20
## 10 2021-11-06 10 Levitating Dua Lipa 56
## # ℹ 330,077 more rows
## Selecciona las columnas que están entre date y artist
## Se cambia la salida del nombre de la columna weeks-on-board
billboard100 %>%
select(date:artist, weeks_popular='weeks-on-board')
## # A tibble: 330,087 × 5
## date rank song artist weeks_popular
## <date> <dbl> <chr> <chr> <dbl>
## 1 2021-11-06 1 Easy On Me Adele 3
## 2 2021-11-06 2 Stay The Kid LAROI & Justin Bieber 16
## 3 2021-11-06 3 Industry Baby Lil Nas X & Jack Harlow 14
## 4 2021-11-06 4 Fancy Like Walker Hayes 19
## 5 2021-11-06 5 Bad Habits Ed Sheeran 18
## 6 2021-11-06 6 Way 2 Sexy Drake Featuring Future & Young … 8
## 7 2021-11-06 7 Shivers Ed Sheeran 7
## 8 2021-11-06 8 Good 4 U Olivia Rodrigo 24
## 9 2021-11-06 9 Need To Know Doja Cat 20
## 10 2021-11-06 10 Levitating Dua Lipa 56
## # ℹ 330,077 more rows
## Selecciona las columnas excepto last-week y peak-rank
billboard100 %>%
select(-'last-week', -'peak-rank')
## # A tibble: 330,087 × 5
## date rank song artist `weeks-on-board`
## <date> <dbl> <chr> <chr> <dbl>
## 1 2021-11-06 1 Easy On Me Adele 3
## 2 2021-11-06 2 Stay The Kid LAROI & Justin Bieber 16
## 3 2021-11-06 3 Industry Baby Lil Nas X & Jack Harlow 14
## 4 2021-11-06 4 Fancy Like Walker Hayes 19
## 5 2021-11-06 5 Bad Habits Ed Sheeran 18
## 6 2021-11-06 6 Way 2 Sexy Drake Featuring Future & You… 8
## 7 2021-11-06 7 Shivers Ed Sheeran 7
## 8 2021-11-06 8 Good 4 U Olivia Rodrigo 24
## 9 2021-11-06 9 Need To Know Doja Cat 20
## 10 2021-11-06 10 Levitating Dua Lipa 56
## # ℹ 330,077 more rows
Mutate
Se utiliza para agregar nuevas columnas o modificar ya
existentes.
Se pueden realizar operaciones aritméticas, aplicar funciones a
columnas existentes y crear nuevas variables basadas en las
existentes
## La función grepl() es equivalente a LIKE en SQL
## Permite buscar patrones en texto, puede ser solo una parte del texto
## En este caso debe cumplir dos condiciones 'Easy' en "song"
## y 'Adele' en "artist"
billboard100 %>%
select(date:artist, weeks_popular='weeks-on-board') %>%
mutate(is_collab = grepl('Easy', song) & grepl('Adele', artist)) %>%
select(song,artist, is_collab, everything())
## # A tibble: 330,087 × 6
## song artist is_collab date rank weeks_popular
## <chr> <chr> <lgl> <date> <dbl> <dbl>
## 1 Easy On Me Adele TRUE 2021-11-06 1 3
## 2 Stay The Kid LAROI & Justi… FALSE 2021-11-06 2 16
## 3 Industry Baby Lil Nas X & Jack Harl… FALSE 2021-11-06 3 14
## 4 Fancy Like Walker Hayes FALSE 2021-11-06 4 19
## 5 Bad Habits Ed Sheeran FALSE 2021-11-06 5 18
## 6 Way 2 Sexy Drake Featuring Futur… FALSE 2021-11-06 6 8
## 7 Shivers Ed Sheeran FALSE 2021-11-06 7 7
## 8 Good 4 U Olivia Rodrigo FALSE 2021-11-06 8 24
## 9 Need To Know Doja Cat FALSE 2021-11-06 9 20
## 10 Levitating Dua Lipa FALSE 2021-11-06 10 56
## # ℹ 330,077 more rows
Filter
Se utiliza para filtrar filas específicas
## Se realiza una selección de solo las artistas 'Shakira' y 'Taylor Swift'
## Además se ondiciona por las que aparecen con valor igual o mayor a 20 en la columna weeks_popular
billboard100 %>%
select(date, rank, song, artist, weeks_popular='weeks-on-board') %>%
filter(weeks_popular >= 20, artist == 'Shakira' | artist == 'Taylor Swift')
## # A tibble: 250 × 5
## date rank song artist weeks_popular
## <date> <dbl> <chr> <chr> <dbl>
## 1 2021-05-08 61 Willow Taylor Swift 20
## 2 2020-01-25 50 Lover Taylor Swift 22
## 3 2020-01-18 36 Lover Taylor Swift 21
## 4 2020-01-11 34 Lover Taylor Swift 20
## 5 2019-11-16 46 You Need To Calm Down Taylor Swift 21
## 6 2019-11-09 52 You Need To Calm Down Taylor Swift 20
## 7 2018-11-17 49 Delicate Taylor Swift 35
## 8 2018-11-10 43 Delicate Taylor Swift 34
## 9 2018-11-03 38 Delicate Taylor Swift 33
## 10 2018-10-27 34 Delicate Taylor Swift 32
## # ℹ 240 more rows
Distinct
Selecciona filas en columnas específicas
## Seleccionó las columnas desde 'date' hasta 'artist'
## Cambió la salida del nombre de la columna 'weeks-on-board'
## Filtró por artista Drake
billboard100 %>%
select(date:artist, weeks_popular='weeks-on-board') %>%
filter(artist == 'Drake')
## # A tibble: 787 × 5
## date rank song artist weeks_popular
## <date> <dbl> <chr> <chr> <dbl>
## 1 2021-11-06 91 No Friends In The Industry Drake 8
## 2 2021-10-30 87 No Friends In The Industry Drake 7
## 3 2021-10-30 90 Champagne Poetry Drake 7
## 4 2021-10-23 74 No Friends In The Industry Drake 6
## 5 2021-10-23 77 Champagne Poetry Drake 6
## 6 2021-10-16 64 No Friends In The Industry Drake 5
## 7 2021-10-16 65 Champagne Poetry Drake 5
## 8 2021-10-16 98 TSU Drake 5
## 9 2021-10-09 54 Champagne Poetry Drake 4
## 10 2021-10-09 60 No Friends In The Industry Drake 4
## # ℹ 777 more rows
## Filtró solo la columna song de la selección anterior
## Creó el vector distinct
distinct <- billboard100 %>%
select(date:artist, weeks_popular='weeks-on-board') %>%
filter(artist == 'Drake') %>%
distinct(song)
Group_by & Summarise
Agrupar un marco de datos por una o más columnas.
Summarise es similar a ‘Mutate’
## group_by permite agrupar por alguna condición, en este caso por "song"
## summarise permite presentar una columna con el resumen según el
## número de semanas que ha estado como la más escuchada
billboard100 %>%
select(date:artist, weeks_popular='weeks-on-board') %>%
filter(artist == 'Drake') %>%
group_by(song) %>%
summarise(total_weeks_popular = mean(weeks_popular))
## # A tibble: 108 × 2
## song total_weeks_popular
## <chr> <dbl>
## 1 0 To 100 / The Catch Up 10.5
## 2 10 Bands 7
## 3 30 For 30 Freestyle 1.5
## 4 6 God 1
## 5 6 Man 1
## 6 7am On Bridle Path 2
## 7 8 Out Of 10 2
## 8 9 3
## 9 9 AM In Dallas 1
## 10 Back To Back 10.5
## # ℹ 98 more rows
Arrange
Permite ordenar las filas de un marco de datos según una o más
columnas
## Selecciono columnas desde "date" hasta "artist"
## Cambio el nombre de la columna "weeks-on-board"
## Filtro por el artista 'Drake'
## Agrupo por "song"
## Genero el resumen por el número máximo de semanas que ha estado en el ranking
## La función arrange agrupa por defecto ordenando en ascendente
## Para que trabaje de forma descendente se le debe escribir desc antes
## de la columna de la condición
## head solo muestra las primeras 10 líneas
billboard100 %>%
select(date:artist, weeks_popular='weeks-on-board') %>%
filter(artist == 'Drake') %>%
group_by(song) %>%
summarise(total_weeks_popular = max(weeks_popular)) %>%
arrange(desc (total_weeks_popular), song) %>%
head(10)
## # A tibble: 10 × 2
## song total_weeks_popular
## <chr> <dbl>
## 1 God's Plan 36
## 2 Hotline Bling 36
## 3 Controlla 26
## 4 Fake Love 25
## 5 Headlines 25
## 6 Nice For What 25
## 7 Best I Ever Had 24
## 8 In My Feelings 22
## 9 Nonstop 22
## 10 Started From The Bottom 22
Count
Se utiliza para contar el número de observaciones por grupos
## Selecciono las columnas desde "date" hasta "artist"
## Cambio el nombre de la columna "weeks-on-board"
## Cuento por la columna "artist" y se cre la columna "n"
## Arregla en orden descendente y crea la columna n
billboard100 %>%
select(date:artist, weeks_popular='weeks-on-board') %>%
count(artist) %>%
arrange(desc(n))
## # A tibble: 10,205 × 2
## artist n
## <chr> <int>
## 1 Taylor Swift 1023
## 2 Elton John 889
## 3 Madonna 857
## 4 Drake 787
## 5 Kenny Chesney 769
## 6 Tim McGraw 731
## 7 Keith Urban 673
## 8 Stevie Wonder 659
## 9 Rod Stewart 657
## 10 Mariah Carey 621
## # ℹ 10,195 more rows