1

I am trying to get a table for my dataset that only shows rows which are above a certain value, but which still uses the numbers in those rows to get the means for the supersets. using the df diamonds, i have the following code. What I want is for all rows with column price less than 3000 to NOT show up, but to still have them figure into the means of price for superset rows of cut and color.

Kind of a separate question, but I'm also trying to figure out how to make it so that if there were to be only one row in any of the subset rows, then they would be on the same line as the superset rows- such as if cut Fair had only one color

library(knitr)
library(kableExtra)

df <- diamonds

a11<-df%>% 
  group_by(cut, color, clarity)%>% 
  summarize_at( .vars=c("price"), 
                .funs=~mean(.,na.rm=TRUE)) %>%
  mutate(sort = 3) 

b11<-df%>%
  group_by(cut, color) %>% 
  summarize_at( .vars=c("price"), 
                .funs=~mean(.,na.rm=TRUE)) %>% 
  mutate(clarity="", sort = 2) %>% 
  select(cut, color, clarity,everything()) 

c11<-df %>% 
  group_by(cut) %>% 
  summarize_at( .vars=c("price"), 
                .funs=~mean(.,na.rm=TRUE)) %>% 
  mutate(color="", clarity= "", sort = 1) %>% 
  select(cut, color,everything()) 

table3<-rbind(a11,b11,c11)
table3%>%
  arrange(cut, color, clarity) %>%
  select(-sort)%>%
  kbl(
    caption = "Table",
    longtable=T
  ) %>%
  kable_paper(full_width = F) %>%
  column_spec(1, bold = T) %>%
  column_spec(3, italic = T) %>%
  collapse_rows(columns = 1:3, valign = "top")%>%
  add_header_above(header = c("Diamonds" = 4))```
sscoresby
  • 67
  • 5

0 Answers0