0

I have a table that I want to use the information there to mix with the below write up using glue package or if there are other packages in r that can handle the situation better I'm open to suggestion

First table

Client Average
Pay Network ₦373.57
Minders ₦321.20
Players ₦306.57
Bronze ₦316.58

The second table

Pay Network Minders Players Bronze
Below Average 23% 50% 60% 90%
Above Average 77% 50% 40% 105

what I want is to create a scenario whereby once the variable name is called in the curly bracket then he should input the values called in the curly bracket using Glue package or any other package that can do the job perfectly.

This is what I want to achieve

here, I want the two highest average values per transaction to be called from the table to enter where the curly bracket is

eg {Minders} and {Play Network} have the highest value per transaction, yet about {23%} and {50%} of their customers made overall purchases below average value in the month of Feb

the output won't of course shows the curly bracket. I just use that as an example the main output will look like this

Minders and Play-Network have the highest value per transaction, yet about 23% and 50% of their customers made overall purchases below average value in the month of Feb

Yomi.blaze93
  • 401
  • 3
  • 10

1 Answers1

0

First, here's your data in a an easier to copy/paste format with all numeric values entered as numbers

dd1 <- read.table(text="Client,Average
Pay Network,373.57
Minders,321.20
Players,306.57
Bronze,316.58", sep=",", header=T)

dd2 <- read.table(text="Group,Pay Network,Minders,Players,Bronze
Below Average,23,50,60,90
Above Average,77,50,40,10", sep=",", header=T, check.names=F)

Then you can combine these tables to get the the info for the summary that you want

library(dplyr)
library(tidyr)

summary <- dd2 %>% 
  pivot_longer(-Group) %>% 
  filter(Group=="Below Average") %>% 
  inner_join(dd1, by=c("name"="Client")) %>% 
  slice_max(Average, n=2)
summary
# A tibble: 2 x 4
#   Group         name        value Average
#   <chr>         <chr>       <int>   <dbl>
# 1 Below Average Pay Network    23    374.
# 2 Below Average Minders        50    321.

Now that we have the output of interest, we can build the string using the summary table.

glue::glue("{summary$name[1]} and {summary$name[2]} have the highest value per transaction, ",
           "yet about {summary$value[1]} and {summary$value[2]} of their customers made overall ", 
           "purchases below average value in the month of Feb")

which will give the string you want.

MrFlick
  • 195,160
  • 17
  • 277
  • 295