tidyerse

df %>%
mutate(GDP_PC = GDP / Pop) %>%
mutate(Group_GDP_PC_Level = case_when(GDP_PC < 3 ~ 'Low',
GDP_PC >= 3 & GDP_PC < 10 ~ 'Medium',
GDP_PC >= 10 ~ 'High')) %>%
filter(Country != 'China') %>%
group_by(Group_GDP_PC_Level) %>%
summarise(Average_GDP_PC_per_group = mean(GDP_PC))

data.table

dt %>%
.[, GDP_PC := GDP / Pop] %>%
.[, Group_GDP_PC_Level := fcase(GDP_PC < 3 , 'Low',
GDP_PC >= 3 & GDP_PC < 10 , 'Medium',
GDP_PC >= 10 , 'High')] %>%
.[Country != 'China', ] %>%
.[, .(Average_GDP_PC_per_group = mean(GDP_PC)), by = .(Group_GDP_PC_Level) ]

codehover code to produce the above

tidyerse

result <- ch_int(type = "incremental") %>% 
          ch_row(text = "df %>% ",
                   img ="./IMG/fixed_dimentions/t0B.png") %>%   
          ch_row(text = "mutate(GDP_PC = GDP / Pop) %>%",
                   img ="./IMG/fixed_dimentions/t1B.png") %>% 
          ch_row(text = "mutate(Group_GDP_PC_Level = case_when(GDP_PC < 3 ~ 'Low',
                            <br><tab3> GDP_PC >= 3 & GDP_PC < 10 ~ 'Medium',<tab3>
                            <br><tab3>GDP_PC >= 10 ~ 'High')) %>%</tab3>",
                   img ="./IMG/fixed_dimentions/t2B.png") %>%   
          ch_row(text = " filter(Country != 'China') %>% ",
                   img ="./IMG/fixed_dimentions/t3B.png") %>% 
          ch_row(text = "group_by(Group_GDP_PC_Level) %>%",
                   img ="./IMG/fixed_dimentions/t4B.png") %>%   
          ch_row(text = "summarise(Average_GDP_PC_per_group = mean(GDP_PC))",
                   img ="./IMG/fixed_dimentions/t4B.png") %>% 
          ch_out(img = "./IMG/fixed_dimentions/t0B.png") 

htmltools::HTML(result)

data.table

result <- ch_int(type = "incremental") %>% 
          ch_row(text = "dt %>% ",
                   img ="./IMG/fixed_dimentions/t0B.png") %>%   
          ch_row(text = ".[, GDP_PC := GDP / Pop] %>% ",
                   img ="./IMG/fixed_dimentions/t1B.png") %>% 
          ch_row(text = ".[, Group_GDP_PC_Level := fcase(GDP_PC < 3 , 'Low',
                             <br><tab3>GDP_PC >= 3 & GDP_PC < 10 , 'Medium',</tab3>
                             <br><tab3>GDP_PC >= 10 , 'High')] %>%</tab3>",
                   img ="./IMG/fixed_dimentions/t2B.png") %>%   
          ch_row(text = ".[Country != 'China', ] %>% ",
                   img ="./IMG/fixed_dimentions/t3B.png") %>% 
          ch_row(text = ".[, .(Average_GDP_PC_per_group = mean(GDP_PC)), by = .(Group_GDP_PC_Level) ]",
                   img ="./IMG/fixed_dimentions/t4B.png") %>%   
          ch_out(img = "./IMG/fixed_dimentions/t0B.png") 

htmltools::HTML(result)