![](/img/trans.png)
[英]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.