0

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)
Tom Tetlaw
  • 83
  • 1
  • 10
  • 21
  • 1
    note that in `group` you only have 1's and 2's and you are asking for subsetting using `data$group <= 100` and `data$group >= 101`, therefore all your data belongs to `group1` and none of them to `group2`, that's why R is returning `NaN` for `mean(group2$score)` – Jilber Urbina Apr 22 '19 at 15:20
  • 1
    I'd strongly recommend you have a look at the [R-FAQ on calculating mean by group](https://stackoverflow.com/q/11562656/903061) and using one of those methods rather than rolling your own. – Gregor Thomas Apr 22 '19 at 15:23

2 Answers2

2

Sorry, just realised that I used $group when I was meant to use $id. Always easier to see mistakes after posting in a public space!

Tom Tetlaw
  • 83
  • 1
  • 10
  • 21
1

This usually means that there is no data in that subset.

mean(numeric(0))
[1] NaN

This is definitely the case with your data as the group variable is either 1 or 2 and you are trying to split them on a value of 100.

James
  • 65,548
  • 14
  • 155
  • 193