ComeçarComece de graça

Refactoring: functions

Copying and pasting once is OK, but three times usually suggests something is wrong.

The provided code calculates an approximate 95% confidence interval for the variables x and y.

Este exercício faz parte do curso

Defensive R Programming

Ver curso

Instruções do exercício

  • Create a function called ci(), taking a single argument called values with no default values.
  • Get the function to return the 95% confidence interval.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

n <- length(x); m <- mean(x); s <- sd(x)
c(m - 1.96 * s / sqrt(n), m + 1.96 * s / sqrt(n))

n <- length(y); m <- mean(y); s <- sd(y)
c(m - 1.96 * s/sqrt(n), m + 1.96 * s/sqrt(n))

# Define a function to prevent pasting the code above
ci <- function(values) {
  n <- length(values)
  m <- ___ 
  s <- ___ 
  c(m - ___, m + ___)
}
Editar e executar o código