-2

Seems like quite an easy problem to solve, but I can't seem to get my head around it in R.

I have dataset with the following columns: 'Biomass' where each row is a value of biomass for a particular species 'Count' where each row is the number of individual animals of that species counted

I need to create a histogram of biomasses, but if I use hist(DF$Biomass) I will get a histogram of the biomasses of the animals where each value is one animal.

I need to include the count, so that I have (for example) the weight frequencies of elephant x 2, giraffe x 56 etc..

DI1
  • 151
  • 1
  • 6
  • 1
    Please show a small reproducible example and expected output for others to test their codes. – akrun Jan 25 '16 at 14:52
  • 1
    Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Heroka Jan 25 '16 at 14:53

2 Answers2

1

you're not making my life easy :)

Is this what you want ?

DF <- data.frame(Biomass=c(200,200,1500),Count = c(36,20,2))
DF2 <- aggregate(Count ~ Biomass,DF,sum) # sum different occurrences for each Biomass value
barplot(DF2$Count,names.arg =DF2$Biomass) # presents them with a barplot, which is more appropriate than an histogram in the R sense here.
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • Hi, thanks for that. I am looking to count the total number of animals in each weight category, but multiplying by count affects the weight category, rather than increasing the count of animals in each weight category. – DI1 Jan 25 '16 at 15:07
0

If I understood you right that is what you need :)

biomass<-c(1,5,7,6,3)

count<-c(1,2,1,3,4)

new<-NULL

for (i in 1:length(biomass))

{

new<-c(new, rep(biomass[i], count[i]))

}

new

hist(new)

So finally just type:

new<-NULL

for (i in 1:length(DF$Biomass))

{

new<-c(new, rep(DF$Biomass[i], DF$Count[i]))

}

hist(new)

Bogdan
  • 864
  • 10
  • 18