Saltar al contenido principal
LibreTexts Español

9.1: Media

  • Page ID
    150514
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    La media se define como la suma de valores dividida por el número de valores que se suman:

    \(\ \bar{X} =\frac{\sum_{i=1}^nx_i}{n}\)

    Digamos que queremos obtener la estatura media para adultos en la base de datos NHANES (contenida en los datos Altura). Sumaríamos las alturas individuales (usando la función sum ()) y luego dividiríamos por el número de valores:

    sum(NHANES$Height)/length(NHANES$Height)
    ## [1] NA

    Esto devuelve el valor NA, porque faltan valores para algunas filas, y la función sum () no los maneja automáticamente. Para abordar esto, podríamos filtrar el marco de datos usando drop_na () para soltar filas con valores NA para esta variable:

    height_noNA <- NHANES %>%
      drop_na(Height) %>%
      pull(Height)
    
    sum(height_noNA)/length(height_noNA)
    ## [1] 160

    Hay, por supuesto, una función incorporada en R llamada mean () que calculará la media. Al igual que la función sum (), mean () devolverá NA si hay algún valor de NA en los datos:

    mean(NHANES$Height)
    ## [1] NA

    La función mean () incluye un argumento opcional llamado na.rm que eliminará los valores NA si se establece en TRUE:

    mean(NHANES$Height, na.rm=TRUE)
    ## [1] 160

    This page titled 9.1: Media is shared under a not declared license and was authored, remixed, and/or curated by Russell A. Poldrack via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.