![]() ![]() In this example, we will find the mean of logical vector using mean() function.Įxample.R – R Program x = c(TRUE, FALSE, FALSE, FALSE, TRUE)įor a logical vector, TRUE is considered as 1 and FALSE is considered as 0.Įxplanation xm = (TRUE + FALSE + FALSE + FALSE + TRUE)/5 =(1+0+0+0+1)/5 = 2/5 = 0.4 Conclusion If NA s are expected in a vector, na.rm has to be considered. And if NA is present in the vector, mean would be NA irrespective of anything else.Įxplanation xm = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 45 + NA) / 9 = NA Na.rm = FALSE does not remove NA values present in vector before calculation proceeds. We will also pass the argument na.rm=FALSE to mean() function. In this example, we will find the mean of numeric vector using mean() function. Na.rm = TRUE removes all the NA values present in vector before calculation proceeds.Įxplanation xm = (2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 45) / 9 = 44 / 8 = 5.5 Example 4 – mean() of Numeric Vector with na.rm=FALSE We will also pass the argument na.rm=TRUE to mean() function.Įxample.R – R Program x = c(1,2,3,4,5,6,7,8,9,45,NA) 1 and 45 are removed and the mean is calculated with rest of elements in the vector.Įxplanation xm = ( 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)/9 = 44/8 = 5.5 Example 3 – mean() of Numeric Vector with na.rm=TRUE Trim=0.10 i.e., 10% of elements at starting and ending are removed. ![]() We will also pass trim attribute to trim off percentage of elements at start and end of the vector. In this example, we will find the mean of numeric vector using mean() function.Įxample.R – R Program x = c(1,2,3,4,5,6,7,8,9,45)Įxplanation xm = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 45)/9 = 90/10 = 9 Example 2 – mean() of Numeric Vector with trim Attribute We shall learn to calculate mean with different options available with mean() function. If TRUE, NA values in vector would be stripped out before mean computation proceeds.Įxamples to Calculate mean of a vector in R trim=0.2 means 20% of elements at the beginning and 20% of the elements at the ending of the vector are removed, and the mean is calculated for the 60% of elements remaining. ![]() It is the fraction of elements that would be dropped before calculating mean. Also x could be complex vector provided time=0.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |