总结将两列除以百分比

时间:2017-11-22 18:59:22

标签: r dplyr divide summarize

我很难找到使用飞机打击数据集在迁移季节死亡的加拿大鹅的百分比。

#airline stats table
airlines <- sd4 %>% 
group_by(STATE) %>% 
filter(SPECIES == "Canada goose" & total_kills > 1) %>% 
mutate(fall_mig_kills = ifelse(SPECIES=="Canada goose" & INCIDENT_MONTH %in% c(9,10,11),total_kills,0)) %>% 
summarise(
pct_mig_kills = fall_mig_kills/total_kills
) %>% 
select(STATE,SPECIES,INCIDENT_MONTH,total_kills,fall_mig_kills,pct_mig_kills)`

这是我收到错误的地方: summarise( pct_mig_kills = fall_mig_kills/total_kills )

,错误是:

Error in summarise_impl(.data, dots) : 
Column `pct_mig_kills` must be length 1 (a summary value), not 10

在分割两个整数列时,我不确定如何获得比长度1更长的值。

任何帮助将不胜感激!

本杰明

1 个答案:

答案 0 :(得分:1)

让我们读取数据,记录所有内容,并查看错误发生的位置。

通常,您应该拥有指向原始数据集的链接或提供缩短版本以遵循reproducibility原则。我在Kaggle上找到了一个aircraft wildlife strikes, 1990-2015数据集,我将在这里使用。 注意: 您需要使用Kaggle帐户下载数据。它也可以在data.gov处获得。

读入数据

library(dplyr)
df <- read.csv("~/../Downloads/database.csv", stringsAsFactors = F)
> df$Species.Name[grepl("Canada goose", df$Species.Name, ignore.case = T)][1]
[1] "CANADA GOOSE"

> names(df)
 [1] "Record.ID"            "Incident.Year"        "Incident.Month"      
 [4] "Incident.Day"         "Operator.ID"          "Operator"            
 [7] "Aircraft"             "Aircraft.Type"        "Aircraft.Make"       
[10] "Aircraft.Model"       "Aircraft.Mass"        "Engine.Make"         
[13] "Engine.Model"         "Engines"              "Engine.Type"         
[16] "Engine1.Position"     "Engine2.Position"     "Engine3.Position"    
[19] "Engine4.Position"     "Airport.ID"           "Airport"             
[22] "State"                "FAA.Region"           "Warning.Issued"      
[25] "Flight.Phase"         "Visibility"           "Precipitation"       
[28] "Height"               "Speed"                "Distance"            
[31] "Species.ID"           "Species.Name"         "Species.Quantity"    
[34] "Flight.Impact"        "Fatalities"           "Injuries"            
[37] "Aircraft.Damage"      "Radome.Strike"        "Radome.Damage"       
[40] "Windshield.Strike"    "Windshield.Damage"    "Nose.Strike"         
[43] "Nose.Damage"          "Engine1.Strike"       "Engine1.Damage"      
[46] "Engine2.Strike"       "Engine2.Damage"       "Engine3.Strike"      
[49] "Engine3.Damage"       "Engine4.Strike"       "Engine4.Damage"      
[52] "Engine.Ingested"      "Propeller.Strike"     "Propeller.Damage"    
[55] "Wing.or.Rotor.Strike" "Wing.or.Rotor.Damage" "Fuselage.Strike"     
[58] "Fuselage.Damage"      "Landing.Gear.Strike"  "Landing.Gear.Damage" 
[61] "Tail.Strike"          "Tail.Damage"          "Lights.Strike"       
[64] "Lights.Damage"        "Other.Strike"         "Other.Damage"        
[67] "totalKills"

请注意,物种名称在所有大写字母中。使用grepl代替==,除非您确定知道这个名字是逐字的。

没有total_kills变量,Fatalities变量代表死亡事件,因此我将忽略该过滤器变量。我找到的是Species.Quantity,这可能就是你要找的东西,也就是事件中遇害的物种总数。

> unique(df$Species.Quantity)
[1] "1"        "2-10"     ""         "11-100"   "Over 100"

对于此示例,我们可以将这些值转换为数值。

> dictNames <- unique(df$Species.Quantity)
> dict <- c(1, 2, 0, 11, 100)
> names(dict) <- dictNames
> dict['1']
1 
1 
> dict['2-10']
2-10 
   2 
> df <- df %>% mutate(totalKills = dict[Species.Quantity])
> table(df$totalKills, useNA = "always")

     1      2     11    100   <NA> 
146563  21852   1166     46   4477 

太好了,现在让我们来看看你的代码吧。

试用您的代码并找到问题

> df %>% 
+   group_by(State) %>% 
+   filter(Species.Name == "CANADA GOOSE" & totalKills > 1) %>% 
+   mutate(fall_mig_kills = ifelse(Species.Name == "CANADA GOOSE" & 
+                                    Incident.Month %in% c(9,10,11),
+                                  totalKills,
+                                  0)
+          ) %>% 
+   summarise(
+     pct_mig_kills = fall_mig_kills/totalKills
+   )
Error in summarise_impl(.data, dots) : 
  Column `pct_mig_kills` must be length 1 (a summary value), not 19
嗯,让我们看看为什么会这样。通过在控制台中输入?summarise来阅读帮助菜单:

  

总结{dplyr} R文档将多个值减少到a   单值

     

描述

     

summarize()通常用于group_by()创建的分组数据。   输出将为每个组分配一行。

好的,因此每个组的输出都会一行由于您已对变量进行了分组,因此我们需要总和杀死。此外,您可能希望创建一个新的变量“inSeason”,它允许您适当地汇总数据。

因此,要解决您的问题,只需添加sum

即可
+   summarise(
+     pct_mig_kills = sum(fall_mig_kills)/sum(totalKills)
+   )
# A tibble: 49 x 2
   State pct_mig_kills
   <chr>         <dbl>
 1          0.70212766
 2    AK    0.50000000
 3    AL    0.00000000
 4    AR    1.00000000
 5    CA    0.06185567

重写您的代码没有错误

现在让我们将其更改为更容易阅读。你关心季节,而不是状态。

> df %>%
+   # inSeason = seasons we care about monitoring
+   # totalKills has NA values, we choose to put deaths at 0
+   mutate(inSeason = ifelse(Incident.Month %in% 9:11, "in", "out"),
+          totalKills = ifelse(is.na(totalKills), 0, totalKills)) %>%
+   # canadian geese only
+   filter(grepl("canada goose", Species.Name, ignore.case = T)) %>%
+   # collect data by inSeason
+   group_by(inSeason) %>%
+   # sum them up
+   summarise(totalDead = sum(totalKills)) %>%
+   # add a ratio value
+   mutate(percentDead = round(100*totalDead/sum(totalDead),0))
# A tibble: 2 x 3
  inSeason totalDead percentDead
     <chr>     <dbl>       <dbl>
1       in       838          34
2      out      1620          66

现在你有赛季与非赛季,总死亡和百分比。如果要添加状态,请将该变量添加到分组中。

另一个注释group_by summarise会自动删除其他列,因此您最后无需使用select

> df %>%
+   mutate(inSeason = ifelse(Incident.Month %in% 9:11, "in", "out"),
+          totalKills = ifelse(is.na(totalKills), 0, totalKills)) %>%
+   filter(grepl("canada goose", Species.Name, ignore.case = T)) %>%
+   group_by(State, inSeason) %>%
+   summarise(totalDead = sum(totalKills)) %>%
+   mutate(percentDead = round(100*totalDead/sum(totalDead),0))
# A tibble: 98 x 4
# Groups:   State [51]
   State inSeason totalDead percentDead
   <chr>    <chr>     <dbl>       <dbl>
 1             in        52          52
 2            out        48          48
 3    AB       in         1          50
 4    AB      out         1          50
 5    AK       in        13          33
 6    AK      out        26          67
 7    AL       in         2          40
 8    AL      out         3          60
 9    AR       in         6         100
10    CA       in        13           8