Skip to content

Instantly share code, notes, and snippets.

@jonocarroll
Created March 14, 2017 02:45
Show Gist options
  • Save jonocarroll/af532f16043856acfe63e331db78c34b to your computer and use it in GitHub Desktop.
Save jonocarroll/af532f16043856acfe63e331db78c34b to your computer and use it in GitHub Desktop.
Historical curiosity: why does var() allow as.character(<numeric>) input?
# using a data.frame with a character column (not uncommon)
d <- data.frame(name = c("alpha", "beta", "gamma"),
x = runif(3),
y = runif(3))
d
#> name x y
#> 1 alpha 0.8577527 0.4611686
#> 2 beta 0.2989644 0.9660795
#> 3 gamma 0.7221540 0.1730588
# trying to take the column-wise mean fails
apply(d, 2, mean)
#> Warning in mean.default(newX[, i], ...): argument is not numeric or
#> logical: returning NA
#> Warning in mean.default(newX[, i], ...): argument is not numeric or
#> logical: returning NA
#> Warning in mean.default(newX[, i], ...): argument is not numeric or
#> logical: returning NA
#> name x y
#> NA NA NA
# since apply converts (expectedly) to a matrix
# and thus coerces to most general type, which
# mean() chokes on
# var() HOWEVER:
apply(d, 2, var)
#> Warning in FUN(newX[, i], ...): NAs introduced by coercion
#> name x y
#> NA 0.08495347 0.16113735
# only complains about the character column,
# and otherwise coerces the numeric columns
# before applying the function
# using the numeric-only part of the data.frame works
apply(d[,-1], 2, mean)
#> x y
#> 0.6262904 0.5334356
apply(d[,-1], 2, var)
#> x y
#> 0.08495348 0.16113734
# Q: why does var() allow character input?
mean(as.character(runif(10)))
#> Warning in mean.default(as.character(runif(10))): argument is not numeric
#> or logical: returning NA
#> [1] NA
var(as.character(runif(10)))
#> [1] 0.04085411
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment