Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. > by(iris[,1:4] , Species , mean)
  2.  
  3. Species: setosa
  4. [1] NA
  5. ------------------------------------------------------------
  6. Species: versicolor
  7. [1] NA
  8. ------------------------------------------------------------
  9. Species: virginica
  10. [1] NA
  11. Warning messages:
  12. 1: In mean.default(data[x, , drop = FALSE], ...) :
  13. argument is not numeric or logical: returning NA
  14.  
  15. 2: In mean.default(data[x, , drop = FALSE], ...) :
  16. argument is not numeric or logical: returning NA
  17.  
  18. 3: In mean.default(data[x, , drop = FALSE], ...) :
  19. argument is not numeric or logical: returning NA
  20.  
  21. R> mean(iris[iris$Species == "setosa", 1:4])
  22. [1] NA
  23. Warning message:
  24. In mean.default(iris[iris$Species == "setosa", 1:4]) :
  25. argument is not numeric or logical: returning NA
  26.  
  27. R> by(iris[,1] , iris$Species , mean)
  28. iris$Species: setosa
  29. [1] 5.006
  30. ------------------------------------------------------------
  31. iris$Species: versicolor
  32. [1] 5.936
  33. ------------------------------------------------------------
  34. iris$Species: virginica
  35. [1] 6.588
  36.  
  37. R> by(iris[,1:4] , iris$Species , colMeans)
  38. iris$Species: setosa
  39. Sepal.Length Sepal.Width Petal.Length Petal.Width
  40. 5.006 3.428 1.462 0.246
  41. ------------------------------------------------------------
  42. iris$Species: versicolor
  43. Sepal.Length Sepal.Width Petal.Length Petal.Width
  44. 5.936 2.770 4.260 1.326
  45. ------------------------------------------------------------
  46. iris$Species: virginica
  47. Sepal.Length Sepal.Width Petal.Length Petal.Width
  48. 6.588 2.974 5.552 2.026
  49.  
  50. foo <- function(x, ...) sapply(x, mean, ...)
  51. by(iris[, 1:4], iris$Species, foo)
  52.  
  53. R> by(iris[, 1:4], iris$Species, foo)
  54. iris$Species: setosa
  55. Sepal.Length Sepal.Width Petal.Length Petal.Width
  56. 5.006 3.428 1.462 0.246
  57. ------------------------------------------------------------
  58. iris$Species: versicolor
  59. Sepal.Length Sepal.Width Petal.Length Petal.Width
  60. 5.936 2.770 4.260 1.326
  61. ------------------------------------------------------------
  62. iris$Species: virginica
  63. Sepal.Length Sepal.Width Petal.Length Petal.Width
  64. 6.588 2.974 5.552 2.026
  65.  
  66. R> with(iris, aggregate(iris[,1:4], list(Species = Species), FUN = mean))
  67. Species Sepal.Length Sepal.Width Petal.Length Petal.Width
  68. 1 setosa 5.006 3.428 1.462 0.246
  69. 2 versicolor 5.936 2.770 4.260 1.326
  70. 3 virginica 6.588 2.974 5.552 2.026
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement