I'm getting a NaN result for mean(group2$score). I'm trying to create a function to list descriptive statistics for my data.
printf <- function (...) {
cat(sprintf(...))
}
data <- data.frame(
id <- c(1:200),
group <- c(replicate(100, 1), replicate(100, 2)),
score <- rnorm(200, mean = 100, sd = 15)
)
descriptives <- function (data) {
group1 <- data[data$group <= 100, ]
group2 <- data[data$group >= 101, ]
printf("group 1 mean: %.2f\n", mean(group1$score))
printf("group 2 mean: %.2f\n", mean(group2$score)) #this is where the NaN gets printed
}
descriptives(data)