EDITEDI have a dataset of healthcare providers, if they provide a certain service, the city they are in, and the year. Some city's have multiple providers. I've constructed it as a time series so there is a data point for each provider and its year (either 2021 or 2023), and then a binary variable for if it provided the service in that year. I want to make a table showing the total number of providers by city in 2021 compared to 2023. The name of the dataset is HCProvidersbyCity
Here is what the data looks like:
structure(list(City = c("Alamosa", "Alamosa", "Albany", "Albany",
"Albany", "Albany", "Albuquerque", "Albuquerque", "Albuquerque",
"Albuquerque", "Albuquerque", "Albuquerque", "Albuquerque", "Albuquerque",
"Alexandria", "Alexandria", "Alexandria", "Alexandria", "Alhambra",
"Alhambra", "Allentown", "Allentown", "Altamonte Springs", "Altamonte Springs",
"Ames", "Ames", "Amherst", "Amherst", "Amsterdam", "Amsterdam",
"Anaheim", "Anaheim", "Anchorage", "Anchorage", "Ann Arbor",
"Ann Arbor", "Ann Arbor", "Ann Arbor", "Ann Arbor", "Ann Arbor",
"Annapolis", "Annapolis", "Antioch", "Antioch", "Arcadia"), Provides_Service = c(1,
0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1), Year = c(21, 23, 21, 23, 21, 23, 21, 23, 21, 23, 21, 23,
21, 23, 21, 23, 21, 23, 21, 23, 21, 23, 21, 23, 23, 21, 21, 23,
21, 23, 21, 23, 21, 23, 21, 23, 21, 23, 21, 23, 21, 23, 21, 21,
21)), row.names = c(NA, -45L), class = c("tbl_df", "tbl", "data.frame"
))
I tried using the code: table(HCProvidersbyCity$City,HCProvidersbyCity$Year,HCProvidersbyCity$Provides_Service)
And I get this: table results
However, the table shows results that don't match the actual data. For example it will say a city like Chicago had 0 providers in both the years 2021 and 2023. I'll post an image of the results I get, but essentially most of the data is turning up as zero. For example, Ann arbor had 2 providers in 2021 and in 2 2023 as well, but is showing a 0 or 2021. Annapolis had 1 providers in 2021 and 1 provider in 2023, but has 0 for both.
I want a table where the City is the right column label, year is the row label, and each year has the number of providers the city had in that year. I did the same thing with Healthcare providers and states, and it worked. I'm looking for something like this, where each city only appears once. state results
I want my data to look like this:
I tried: t(table(HCProvidersbyCity[HCProvidersbyCity$Provides_Service == 1,])[,1,])
and it gave me this: !results from tt(table(HCProvidersbyCity[HCProvidersbyCity$Provides_Service == 1,])[,1,])]4