0

I created a tibble named fish. Using the %>% operator, then created an object called a result that should take fish to then group entries by the species of fish and then compute the mean eye size of each species, but it is not coming out how it should. What am I doing wrong?

set.seed(23)
fish = tibble(
    species=c(rep("toadfish", 333), rep("frogfish",333), rep("batfish", 334)),
     body_size=rnorm(1000),
     eye_size=abs(0.022*rnorm(1000)/body_size),
     brain_volume=body_size^2*eye_size/.6
    )
 
result <- fish %>% mean(fish$eye_size)

user15508217
  • 11
  • 1
  • 3

2 Answers2

2

You don't have to create result and can do it in one step with piping.

fish %>% 
  group_by(species) %>%
  summarise(mean = mean(eye_size), .groups = "drop")

Edited to include @LMc's tip.

tonybot
  • 643
  • 2
  • 10
0

In base R you can use the aggregate function:

with(fish, aggregate(eye_size, list(species), mean))
LMc
  • 12,577
  • 3
  • 31
  • 43