0

So I have cattle, and a lot of them. I'm trying to add a column to my dataframe that is the total number of cattle per day, based on several counts summed over the course of a single day. I have this dataframe:

Guirvidig

# A tibble: 494 × 8
#    Year  Week       Date  Location Animals           From            To     Notes
#   <int> <int>     <date>     <chr>   <int>          <chr>         <chr>     <chr>
#    2010   31 2010-08-01  GUIRVIDIG    580 Bongor – Tchad BANKI NIGERIA       RAS

And what I'm getting is:

Guirvidig$TotalAnimals <- aggregate(Animals~Date,Guirvidig,sum)

Error in $<-.data.frame(*tmp*, TotalAnimals, value = list(Date = c(14822, : replacement has 136 rows, data has 494`

Of course the data has more rows, as the replacement is the sum of the rows from the original. How can I have the replacement row be the same for every single row of the same day or just show the sum for the day at the first row for that day? Any help will be appreciated.

Sotos
  • 51,121
  • 6
  • 32
  • 66
Josiah
  • 3
  • 2
  • 1
    Welcome to SO. Please have a look at [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also add expected output. – Sotos Jun 12 '17 at 15:15

2 Answers2

0
library(dplyr)

Guirvidig <- Guirvidig %>% group_by(date) %>% mutate(Total_Animals = sum(Animals) %>% ungroup()
A.Kot
  • 7,615
  • 2
  • 22
  • 24
0

You can just wrap aggregate with merge:

merge(aggregate(Animals ~ Date, Guirvidig, sum), Guirvidig)
ssp3nc3r
  • 3,662
  • 2
  • 13
  • 23