[英]Identify value in 1 column corresponding to equal values in two another columns by row
[英]Summarise columns and identify columns where value is not equal to 100
我有这个名为Wild_animals
的 Dataframe,如下所示:
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
我想弄清楚的是总结每一行,这样我就可以通过 R 确定当年是否有 100 只动物,或者高于或低于该年。
小标题应该是这样的:
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
整齐的格式
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.