0

I have a big dataset. There are 25,500 rows within two columns in the dataset. First Column is species name and the second column is altitude(m). It is part of the dataset

. .

As you see, every species usually has multiple altitudes. So species names are repeated in several rows. I want to average altitudes for every species (unique species names) and get an output like the one below What should I do? enter image description here

Sina Kh.
  • 17
  • 3
  • Does this answer your question? [Grouping functions (tapply, by, aggregate) and the \*apply family](https://stackoverflow.com/questions/3505701/grouping-functions-tapply-by-aggregate-and-the-apply-family) – user2974951 Jan 19 '23 at 07:00
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 19 '23 at 08:02

1 Answers1

0

Here is and example of how you can do it in base r:


# Create a test df. you need to import your own dateset into r
df <- data.frame(Species=c('a', 'a', 'b', 'b', 'c', 'b', 'c'),
                 ALT=c(5, 8, 14, 18, 5, 7, 7))



#find mean ALT by species
aggregate(df$ALT, list(df$Species), FUN=mean)

# Group.1    x
#1       a  6.5
#2       b 13.0
#3       c  6.0

Or dplyr

library(dplyr)

df %>%
  group_by(Species) %>%
  summarise_at(vars(ALT), mean)

#  Species   ALT
#  <chr>   <dbl>
#1 a         6.5
#2 b        13  
#3 c         6 
asaei
  • 491
  • 3
  • 5