我试图通过将数据按三列分组来减少数据框。原始数据为:
dput(coupler)
structure(list(Category = c("SSC", "SSC", "SSC", "SSC", "SSC",
"SSC", "SSC", "SSC", "HBC", "HBC", "HBC", "HBC", "HBC", "TC",
"TC", "TC", "TC", "TC", "TC", "BSC", "BSC", "BSC", "BSC", "BSC",
"BSC", "BSC", "GSC", "GSC", "GSC", "GSC", "GSC", "GSC", "GSC",
"GSC", "GSC", "GSC", "GSC", "GSC", "GSC", "GSC"), `Bar Size` = c("No. 6",
"No. 8", "No. 6", "No. 8", "No. 4", "No. 10", "No. 8", "No. 4",
"No. 10", "No. 8", "No. 5", "No. 8", "No. 10", "No. 18", "No. 10",
"No. 10", "No. 5", "No. 8", "No. 10", "No. 18", "No. 18", "No. 8",
"No. 5", "No. 8", "No. 8", "No. 10", "No. 8", "No. 6", "No. 8",
"No. 10", "No. 10", "No. 10", "No. 11", "No. 18", "No. 18", "No. 5",
"No. 5", "No. 8", "No. 10", "No. 10"), `No. Bars` = c(12, 12,
28, 28, 4, 9, 10, 9, 9, 6, 6, 6, 6, 4, 14, 4, 18, 18, 18, 2,
2, 3, 12, 1, 11, 12, 6, 3, 6, 3, 2, 4, 3, 4, 1, 8, 10, 18, 16,
2), Failure = c("Bar pullout", "Bar pullout", "Bar fracture",
"Bar fracture", "Bar fracture", "Bar fracture", "Bar fracture",
"Bar fracture", "Bar fracture", "Bar fracture", "Bar fracture",
"Bar fracture", "Bar fracture", "Coupler failure", "Bar fracture",
"Coupler failure", "Bar fracture", "Bar fracture", "Bar fracture",
"Bar pullout", "Bar fracture", "Bar fracture", "Bar fracture",
"Coupler failure", "Bar fracture", "Bar fracture", "Bar pullout",
"Coupler failure", "Bar fracture", "Bar fracture", "Bar pullout",
"Coupler failure", "Coupler failure", "Bar fracture", "Coupler failure",
"Bar fracture", "Bar pullout", "Bar fracture", "Bar fracture",
"Coupler failure"), Researcher = c("Lloyd", "Lloyd", "Lloyd",
"Lloyd", "Hillis & Saiidi", "Rowell et al.", "Huaco & Jirsa",
"Alam et al.", "Rowell et al.", "Haber et al.", "Tazarv", "Tazarv",
"Tazarv", "Noureddine", "Rowell et al.", "Rowell et al.", "Tazarv",
"Tazarv", "Tazarv", "Noureddine", "Noureddine", "Yang et al.",
"Tazarv", "Tazarv", "Tazarv", "Tazarv", "Ameli et al.", "Jansson",
"Haber et al.", "Rowell et al.", "Rowell et al.", "Rowell et al.",
"Jansson", "Noureddine", "Noureddine", "Tazarv", "Tazarv", "Tazarv",
"Tazarv", "Tazarv"), Year = c(2001, 2001, 2001, 2001, 2009, 2009,
2012, 2010, 2009, 2013, 2020, 2020, 2020, 1996, 2009, 2009, 2020,
2020, 2020, 1996, 1996, 2014, 2020, 2020, 2020, 2020, 2015, 2008,
2013, 2009, 2009, 2009, 2008, 1996, 1996, 2020, 2020, 2020, 2020,
2020)), row.names = c(NA, -40L), class = c("tbl_df", "tbl", "data.frame"
))
我使用以下行:
coupler.graph <- coupler[,1:4] %>% group_by(Category, `Bar Size`, Failure) %>%
filter(`No. Bars` == sum(`No. Bars`))
但是结果数据框缺少一些行。我不确定为什么会这样。如何保留所有行并仅汇总共同的行。