Escriba la linea del codigo para mostrar el numero de estados por región

#install.packages("dslabs")
library(dslabs)
data("murders")

Contar el número de estados por región

estado_por_region <- murders %>% group_by(region) %>% summarize(num_estados = n_distinct(state))

Mostrar el resultado

print(estado_por_region)

#install.packages("dplyr")
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
estado_por_region <- murders %>%
  group_by(region) %>%
  summarize(num_estados = n_distinct(state))
print(estado_por_region)
## # A tibble: 4 × 2
##   region        num_estados
##   <fct>               <int>
## 1 Northeast               9
## 2 South                  17
## 3 North Central          12
## 4 West                   13
install.packages("dslabs")
## Warning: package 'dslabs' is in use and will not be installed
library(dslabs)
data(movielens)
head(movielens)
##   movieId                                   title year
## 1      31                         Dangerous Minds 1995
## 2    1029                                   Dumbo 1941
## 3    1061                                Sleepers 1996
## 4    1129                    Escape from New York 1981
## 5    1172 Cinema Paradiso (Nuovo cinema Paradiso) 1989
## 6    1263                        Deer Hunter, The 1978
##                             genres userId rating  timestamp
## 1                            Drama      1    2.5 1260759144
## 2 Animation|Children|Drama|Musical      1    3.0 1260759179
## 3                         Thriller      1    3.0 1260759182
## 4 Action|Adventure|Sci-Fi|Thriller      1    2.0 1260759185
## 5                            Drama      1    4.0 1260759205
## 6                        Drama|War      1    2.0 1260759151

Numero de fila

num_filas <-nrow(movielens)
print(num_filas)
## [1] 100004

Numero de Variables

num_variables <-ncol(movielens)
print(num_variables)
## [1] 7

Las variables

names(movielens)
## [1] "movieId"   "title"     "year"      "genres"    "userId"    "rating"   
## [7] "timestamp"

Tipo de Variable es Title

class(movielens$title)
## [1] "character"

Tipo de Variable es Genres

class(movielens$genres)
## [1] "factor"

Cuantos niveles hay en la variable genres (cuando no es Factor)

genres_factor <- factor(movielens$genres)

num_niveles <- nlevels(genres_factor)
print(num_niveles)
## [1] 901

Cuando es Factor

nlevels(movielens$genres)
## [1] 901

Operador $

pop <-data.frame(murders$population)

pop <-data.frame(murders$population)
pop
##    murders.population
## 1             4779736
## 2              710231
## 3             6392017
## 4             2915918
## 5            37253956
## 6             5029196
## 7             3574097
## 8              897934
## 9              601723
## 10           19687653
## 11            9920000
## 12            1360301
## 13            1567582
## 14           12830632
## 15            6483802
## 16            3046355
## 17            2853118
## 18            4339367
## 19            4533372
## 20            1328361
## 21            5773552
## 22            6547629
## 23            9883640
## 24            5303925
## 25            2967297
## 26            5988927
## 27             989415
## 28            1826341
## 29            2700551
## 30            1316470
## 31            8791894
## 32            2059179
## 33           19378102
## 34            9535483
## 35             672591
## 36           11536504
## 37            3751351
## 38            3831074
## 39           12702379
## 40            1052567
## 41            4625364
## 42             814180
## 43            6346105
## 44           25145561
## 45            2763885
## 46             625741
## 47            8001024
## 48            6724540
## 49            1852994
## 50            5686986
## 51             563626
pop_min<- min(murders$population)
murders$population[pop_min]
## [1] NA
pop_min
## [1] 563626