Home > database >  How can I calculate the mean and standard deviation of a column in a data frame using the dplyr pack
How can I calculate the mean and standard deviation of a column in a data frame using the dplyr pack

Time:01-13

An Example:

library(dplyr)
data <- data.frame(x = rnorm(10))
mean(data$x)
sd(data$x)

And it gives me an error. Why?

CodePudding user response:

The code you provided is running perfectly. However, I still want to give you the dplyr solution that I find most intuitive.

library(tidyverse)

data.frame(x = rnorm(10)) %>%
  summarise(sd= sd(x),
            mean= mean(x))
#>          sd        mean
#> 1 0.8055597 -0.01786294

Created on 2023-01-13 with reprex v2.0.2

CodePudding user response:

the provided code doesn't produce error!

  • Related