I have a list of cities and within them a certain number of both open and closed stores. I want to use dplyr to find the percentage of closed stores for each city. Store.status is either "A" if active/open or "I" if inactive/closed.
So far I have:
df %>%
group_by(City,Store.Status) %>%
summarise(Num_stores=n(),
Percent_closed = ??? / Num_stores) %>%
arrange(desc(Num_stores))
Output:
City Store Status Num_store Percent_closed
--------------------------------------------------------------
Chicago O 88 (???/88)
Denver O 78 (???/78)
New York O 56 (???/56)
I'm getting back just the total number of stores in each city along with an "0" in the store status even though it's the sum of all open and closed stores. I don't know how to find the percentage of closed stores in each city, which would simply be the total number of closed stores divided by the total stores in the city. I know this is really simple but I'm new to R/coding in general and can't find anything that will help.
Ideally, the output would be :
City Num_store Percent_closed
--------------------------------------------------------------
Ackley 3 (1/3)
Adair 2 (0/2)
Where Num_store is the total number of stores, active or inactive. Percent_closed is the total number inactive("I" in Store.Status) over the total number of stores.
For example, there's two Ackley stores with an "A" and one with "I" so 1 inactive / 3 total.
> dput(head(iowa, 20))
structure(list(Store = c(2656L, 2657L, 2674L, 2835L, 2954L, 3013L,
3041L, 3045L, 3162L, 3354L, 3385L, 3886L, 3738L, 3740L, 3831L,
3833L, 3838L, 3872L, 3873L, 3879L), Name = c("Hy-Vee Food Store / Corning",
"Hy-Vee Food Store / Bedford", "Hy-Vee Food Store / Lamoni",
"CVS Pharmacy #8538 / Cedar Falls", "Dahl's / Ingersoll", "Keith's Foods",
"Shugar's Super Valu / Colfax", "Britt Food Center", "Nash Finch / Wholesale Food",
"Sam's Club 8238 / Davenport", "Sam's Club 8162 / Cedar Rapids",
"Wal-Mart 0646 / Anamosa", "Hartig Drug Company #4 / Dubuque",
"Hometown Foods / Waterloo", "The Market Of Madrid", "Wal-Mart 3394 / Atlantic",
"Schnucks / Bettendorf", "Target Store T-0086 / Dubuque", "Target Store T-1113 / Coralville",
"Target Store T-1800 / Sioux City"), Store.Status = c("A", "A",
"A", "A", "I", "A", "A", "A", "I", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A"), Address = c("300 10th St", "1604 Bent",
"720 East Main", "2302 West First St", "3425 Ingersoll", "207 E Locust St",
"28 E Howard", "8 2nd St NW", "807 Grandview", "3845 Elmore Ave.",
"2605 Blairs Ferry Rd NE", "101 115 St", "2225 Central Ave",
"1010 E Mitchell Ave", "301 Annex Rd", "1905 East 7th St", "858 Middle Rd",
"3500 Dodge St", "1441 Coral Ridge Ave", "5775 Sunnybrook Dr"
), City = c("Corning", "Bedford", "Lamoni", "Cedar Falls", "Des Moines",
"Bloomfield", "Colfax", "Britt", "Muscatine", "Davenport", "Cedar Rapids",
"Anamosa", "Dubuque", "Waterloo", "Madrid", "Atlantic", "Bettendorf",
"Dubuque", "Coralville", "Sioux City"), State = c("IA", "IA",
"IA", "IA", "IA", "IA", "IA", "IA", "IA", "IA", "IA", "IA", "IA",
"IA", "IA", "IA", "IA", "IA", "IA", "IA"), Zip.Code = c("51632",
"50833", "50140", "50613", "50300", "52537", "50054", "50423",
"52761", "52807", "52402", "52205", "52001", "50702", "50156",
"50022", "52722", "52003", "52241", "51106"), Store.Address = c("300 10th St\nCorning, IA 51632\n(40.991861, -94.731809)",
"1604 Bent\nBedford, IA 50833\n(40.676171, -94.725578)", "720 East Main\nLamoni, IA 50140\n(40.623647, -93.924475)",
"2302 West First St\nCedar Falls, IA 50613\n(42.539874, -92.472778)",
"3425 Ingersoll\nDes Moines, IA 50300\n(41.586313, -93.663337)",
"207 E Locust St\nBloomfield, IA 52537\n(40.752691, -92.412847)",
"28 E Howard\nColfax, IA 50054\n(41.677932, -93.244443)", "8 2nd St NW\nBritt, IA 50423\n(43.098696, -93.801917)",
"807 Grandview\nMuscatine, IA 52761\n(41.408437, -91.064113)",
"3845 Elmore Ave.\nDavenport, IA 52807\n(41.559731, -90.527081)",
"2605 Blairs Ferry Rd NE\nCedar Rapids, IA 52402\n(42.031819, -91.67969)",
"101 115 St\nAnamosa, IA 52205", "2225 Central Ave\nDubuque, IA 52001\n(42.51384, -90.671531)",
"1010 E Mitchell Ave\nWaterloo, IA 50702\n(42.476639, -92.343118)",
"301 Annex Rd\nMadrid, IA 50156\n(41.87894, -93.815365)", "1905 East 7th St\nAtlantic, IA 50022\n(41.403853, -94.98571)",
"858 Middle Rd\nBettendorf, IA 52722\n(41.539421, -90.520013)",
"3500 Dodge St\nDubuque, IA 52003\n(42.491944, -90.720589)",
"1441 Coral Ridge Ave\nCoralville, IA 52241\n(41.691276, -91.608399)",
"5775 Sunnybrook Dr\nSioux City, IA 51106\n(42.448939, -96.332446)"
), Report.Date = c("10/01/2018", "10/01/2018", "10/01/2018",
"10/01/2018", "10/01/2018", "10/01/2018", "10/01/2018", "10/01/2018",
"10/01/2018", "10/01/2018", "10/01/2018", "10/01/2018", "10/01/2018",
"10/01/2018", "10/01/2018", "10/01/2018", "10/01/2018", "10/01/2018",
"10/01/2018", "10/01/2018"), Inactive = c(FALSE, FALSE, FALSE,
FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)), row.names = c(NA,
20L), class = "data.frame")