Page 275 - Data Science class 11
P. 275

7.11.1 Mean

            It is calculated by taking the sum of the values and dividing them by the number of values in a data series.
            In R, the function mean() is used to calculate this.
            Syntax

            The basic syntax for calculating the mean in R is:
                                          mean(x, trim = 0, na.rm = FALSE,...)
            The following is a description of the parameters used:
               • x: is the input vector.
               • trim: is used to remove some observations from both ends of the sorted vector.

               • na.rm: is used to remove the missing values from the input vector.
            Example
            Enter the following code snippet in the script panel:

                # Creating a vector.
                x <- c(13,7,3,4.9,19,3,45,-20,9,-4)
                # Finding Mean.
                result.mean <- mean(x)
                print(result.mean)
            When we execute the above code, it produces the following result:





















            Applying Trim Option
            When the trim parameter is supplied, the values in the vector get sorted, and then the required numbers of observations
            are dropped from calculating the mean.

            When trim = 0.3, 3 values from each end will be dropped from the calculations to find the mean.
            In this case, the sorted vector is (-20,-4,3,3,4.9,7,13,19,45) and the values removed from the vector for calculating the
            mean are (−20,−4,3) from the left and (13,19,45) from the right.
            Example
            Enter the following code snippet in the script panel:

                # Create a vector.

                x <- c(13,7,3,4.9,19,3,45,-20,9,-4)
                # Find Mean.




                                                           Coding for Data Science Visualisation using R-Studio  273
   270   271   272   273   274   275   276   277   278   279   280