How to build a table with kableExtra with summarized months but not summarize the values of the other columns

242 Views Asked by At

I want to have a table that contains all my columns.

The dataframe looks like this:

dataframe

So I want a table that just shows the state, party, population, cases, deaths, cases/100K. without the Date but with the values from the latest Day in the Dataset, which would be today the 2020-09-29.

I tried it like this:

corona_cases_month <- corona_party_states %>% group_by(state, Party, Date=floor_date(Date)) %>% rename("Population"=totalPopulation, "Confirmed Cases"=totalConfirmed, "Confirmed Deaths"=totalDeaths, "Cases/100K"=CasesPop)

corona_cases_month %>%
kbl() %>%
  kable_paper("hover", full_width = F) %>%
    kable_styling(fixed_thead = T) %>%
   column_spec(1, bold = T, border_right = T) %>%
column_spec(6, bold = T, color = "orange")

but it's not the right outcome because it still shows every date in a new row.

I tried it with the floor function before and it worked - but I used summarize and of course the values of population, cases, deaths than were all wrong:

tableoutput

How can I build this table with only the values from yesterday?

1

There are 1 best solutions below

3
On BEST ANSWER

I would recommend filtering the data to only keep the rows with the last date. I wouldn't try to create a new variable in the group_by instead do it in a mutate. This is one way:

corona_cases_month <- corona_party_states %>% 
  group_by(state, Party) %>% 
  mutate(Date_new=floor_date(Date)) %>% 
  filter(Date == Date_new) %>% 
  ungroup() %>% 
  group_by(state, Party, Date_new) %>%
  rename("Population"=totalPopulation, "Confirmed Cases"=totalConfirmed, "Confirmed Deaths"=totalDeaths, "Cases/100K"=CasesPop)