The median and mean are two commonly used measures of central tendency in statistics. The median is the middle value of a data set, while the mean is the average value of a data set.
In R, you can use the median() and mean() functions to calculate the median and mean of a data set. These functions take a vector of values as input and return the median and mean as output. For example, the following code calculates the median and mean of the values in the "x" vector:
x <- c(1, 2, 3, 4, 5)
median(x)
mean(x)
In this example, the median of the "x" vector is 3, and the mean is 3. This means that 3 is the middle value of the "x" vector, and the average value of the "x" vector is 3.
You can also use the median() and mean() functions on a column of a data frame to calculate the median and mean of the values in that column. For example, the following code calculates the median and mean of the "x" column in the "mydata" data frame:
mydata <- data.frame(x = c(1, 2, 3, 4, 5),
y = c(6, 7, 8, 9, 10))
median(mydata$x)
mean(mydata$x)
In this example, the median of the "x" column in the "mydata" data frame is 3, and the mean is 3. This means that 3 is the middle value of the "x" column, and the average value of the "x" column is 3.
No comments:
Post a Comment