-3

I am providing a sample of my data as under:

Species Individuals  
A   1  
A   2  
A   3  
B   1  
B   2  
B   3  
C   1  
C   2  
C   3  
A   1  
B   2  
C   3  
C   1  
B   2  
B   3  
A   1  

I am expecting the output to be:

Species  Frequency  
A               8  
B              13  
C              10  

Is it possible to do this using R ?

talat
  • 68,970
  • 21
  • 126
  • 157
Anurag Mishra
  • 1,007
  • 6
  • 16
  • 23
  • 1
    You can find all the examples of how to do this operation in the link Henrik provided. They are almost exactly the same. – talat Dec 16 '14 at 14:01

2 Answers2

2

Try

aggregate(cbind(Frequency=Individuals)~Species,df, sum)
#    Species Frequency
#1       A         8
#2       B        13
#3       C        10

Or

library(data.table)
setDT(df)[, list(Frequency=sum(Individuals)), by=Species]
#     Species Frequency
#1:       A         8
#2:       B        13
#3:       C        10
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You may find the intuitive flow of dplyr pleasing. The added advantage is that the result still is data frame.

library(dplyr)
your_data %>% group_by(Species) %>% summarise(Frequency=sum(Individuals))
#   Species Individuals
# 1       A           8
# 2       B          13
# 3       C          10
KFB
  • 3,501
  • 3
  • 15
  • 18