I have this Dataframe called Wild_animals
that looks as following:
Animal 2000 2001 2002 2003 2004 2005 2006 2007
Cat 25 40 20 0 51 15 85 0
Dog 25 15 20 0 48 46 5 0
Fox 25 12 20 0 0 15 5 0
Tiger 10 21 20 0 0 15 1 0
Lion 5 7 20 100 1 40 4 0
What I'm trying to figure out is to summarise each row so I can determine through R if there is 100 animals that year, or above or below that.
The tibble should look something like this:
2000 2001 2002 2003 2004 2005 2006 2007
90 95 100 100 100 131 100 0
library(tidyverse)
df %>%
summarise(across(2:ncol(.), sum))
# A tibble: 1 x 8
`2000` `2001` `2002` `2003` `2004` `2005` `2006` `2007`
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 90 95 100 100 100 131 100 0
Tidy format
df %>%
summarise(across(2:ncol(.), sum)) %>%
pivot_longer(everything(),
names_to = "year",
values_to = "sum")
# A tibble: 8 x 2
year sum
<chr> <dbl>
1 2000 90
2 2001 95
3 2002 100
4 2003 100
5 2004 100
6 2005 131
7 2006 100
8 2007 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.