Home > Mobile >  Applying a function given a set of points with group by in tidyverse
Applying a function given a set of points with group by in tidyverse

Time:12-21

I have a dataset where I want to apply the following function with group_by

df %>%
  group_by(ci_subtype_c_by) %>%
  summarize(total_time_hours = sum(handle_time_hours)) %>%
  mutate(ahmdal_A = 1 / ((1 - total_time_hours/total_hours)   ((total_time_hours/total_hours) / 2))) %>%
  arrange(desc(ahmdal_A))

Here, the factor I'm applying is 2 to create a new column, and this ouputs correctly:

# A tibble: 63 × 3
   ci_subtype_c_by          total_time_hours ahmdal_A
   <chr>                               <dbl>    <dbl>
 1 Web Based Application             458054.     1.24
 2 #N/B                              100002.     1.04
 3 Desktop Application                54992.     1.02
 4 Laptop                             51768.     1.02
 5 SAP                                47887.     1.02
 6 SAN                                25001.     1.01
 7 Desktop                            16781.     1.01
 8 Standard Application               15032.     1.01
 9 Client Based Application           13229.     1.01
10 Banking Device                     11085.     1.00

However, I would like to be able to test different results with other factors. Let's say, a range between 1.2 and 5.

Then, having everything in a tibble, I would like to plot a graph to check the curve that the function does for each ci_subtype_c_by applying a different factor. However, I don't know how to apply this function for a range of parameters with group by and summarize, and which would be the best way to store it for plotting.

Thanks.

The df

     dput(df %>% slice_sample(n=50))
structure(list(case_id = c("IM0021507", "IM0009022", "IM0042249", 
"IM0021079", "IM0015085", "IM0044183", "IM0042686", "IM0030532", 
"IM0005129", "IM0026802", "IM0018455", "IM0043452", "IM0016814", 
"IM0041263", "IM0021260", "IM0027965", "IM0019772", "IM0006998", 
"IM0019250", "IM0025967", "IM0010696", "IM0035433", "IM0024881", 
"IM0015464", "IM0042952", "IM0042178", "IM0035318", "IM0003592", 
"IM0012140", "IM0034781", "IM0027556", "IM0044687", "IM0035342", 
"IM0038641", "IM0014723", "IM0030615", "IM0031352", "IM0023150", 
"IM0003091", "IM0039715", "IM0012989", "IM0038909", "IM0013061", 
"IM0035510", "IM0043981", "IM0018950", "IM0010633", "IM0011105", 
"IM0024787", "IM0008484"), activity = c("Closed", "Closed", "Closed", 
"Closed", "Closed", "Closed", "Closed", "Closed", "Closed", "Closed", 
"Closed", "Closed", "Closed", "Closed", "Closed", "Closed", "Closed", 
"Closed", "Closed", "Closed", "Closed", "Closed", "Closed", "Closed", 
"Closed", "Closed", "Closed", "Closed", "Closed", "Closed", "Closed", 
"Closed", "Closed", "Closed", "Closed", "Closed", "Closed", "Closed", 
"Closed", "Closed", "Closed", "Closed", "Closed", "Closed", "Closed", 
"Closed", "Closed", "Closed", "Closed", "Closed"), start_timestamp = structure(c(1386933454, 
1383044440, 1394447086, 1386845853, 1384937850, 1395136432, 1394531309, 
1390470786, 1381760888, 1389266836, 1385996538, 1394784761, 1385464520, 
1394016593, 1386859460, 1389699105, 1386344496, 1382361588, 1386171020, 
1389028207, 1383641782, 1392025771, 1388502788, 1385027448, 1394615107, 
1394439165, 1391786411, 1381231494, 1383923146, 1391695078, 1389610553, 
1395237955, 1392023403, 1392972600, 1384850929, 1390485259, 1390821300, 
1387470466, 1381149819, 1393411646, 1384269510, 1393200757, 1384272078, 
1392029781, 1395056987, 1386148110, 1383641774, 1383663603, 1388486627, 
1382956499), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    complete_timestamp = structure(c(1386944261, 1383120060, 
    1395845002, 1386849796, 1385041235, 1395142139, 1394532727, 
    1390471381, 1381840241, 1389288750, 1386243172, 1394786835, 
    1385480271, 1394024296, 1386863848, 1389707957, 1386347896, 
    1382439633, 1386231624, 1389273059, 1383646239, 1392368388, 
    1388648163, 1385027576, 1395330007, 1394467545, 1392023679, 
    1381737717, 1384180223, 1392587995, 1389779741, 1395242753, 
    1392023652, 1393239220, 1384865660, 1390490138, 1390838828, 
    1387473632, 1381753681, 1393599704, 1384332859, 1393229418, 
    1384356191, 1392124220, 1395058516, 1386150601, 1384279156, 
    1383813721, 1388494785, 1383739145), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), variant = c("Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1", "Variant 1", "Variant 1", "Variant 1", 
    "Variant 1", "Variant 1"), variant_index = c(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, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1), ci_name_aff = c("WBA000144", "WBA000144", 
    "SBA000142", "SUB000456", "WBA000145", "DTA000056", "WBA000058", 
    "SBA000173", "PRN000140", "SBA000659", "WBA000133", "SUB000113", 
    "SAP000005", "SUB000426", "WBA000011", "DTA000302", "SBA000010", 
    "DTA000027", "SUB000456", "WBA000103", "WBA000058", "MON00015", 
    "SBA000010", "DTA000035", "SBA000458", "CBD000471", "SAP000005", 
    "LAP000446", "SBA000782", "WBA000148", "WBA000133", "SBA000461", 
    "WBA000133", "APP000004", "WSR001354", "SUB000456", "DTA000016", 
    "WBA000044", "SBA000101", "DSK000006", "WBA000144", "SSW000385", 
    "SBA000607", "SUB000443", "WBA000018", "SBA000427", "LAP000018", 
    "LAP000055", "SUB000113", "SBA000074"), ci_type_aff = c("application", 
    "application", "application", "subapplication", "application", 
    "application", "application", "application", "officeelectronics", 
    "application", "application", "subapplication", "application", 
    "subapplication", "application", "application", "application", 
    "application", "subapplication", "application", "application", 
    "displaydevice", "application", "application", "application", 
    "computer", "application", "computer", "application", "application", 
    "application", "application", "application", "application", 
    "computer", "subapplication", "application", "application", 
    "application", "computer", "application", "software", "application", 
    "subapplication", "application", "application", "computer", 
    "computer", "subapplication", "application"), ci_subtype_aff = c("Web Based Application", 
    "Web Based Application", "Server Based Application", "Web Based Application", 
    "Web Based Application", "Desktop Application", "Web Based Application", 
    "Server Based Application", "Printer", "Server Based Application", 
    "Web Based Application", "Server Based Application", "SAP", 
    "Web Based Application", "Web Based Application", "Desktop Application", 
    "Server Based Application", "Desktop Application", "Web Based Application", 
    "Web Based Application", "Web Based Application", "Monitor", 
    "Server Based Application", "Desktop Application", "Server Based Application", 
    "Banking Device", "SAP", "Laptop", "Server Based Application", 
    "Web Based Application", "Web Based Application", "Server Based Application", 
    "Web Based Application", "Citrix", "Windows Server", "Web Based Application", 
    "Desktop Application", "Web Based Application", "Server Based Application", 
    "Desktop", "Web Based Application", "System Software", "Server Based Application", 
    "Web Based Application", "Web Based Application", "Server Based Application", 
    "Laptop", "Laptop", "Server Based Application", "Server Based Application"
    ), service_component_wbs_aff = c("WBS000318", "WBS000318", 
    "WBS000199", "WBS000073", "WBS000318", "WBS000091", "WBS000073", 
    "WBS000163", "WBS000096", "WBS000255", "WBS000073", "WBS000095", 
    "WBS000271", "WBS000073", "WBS000152", "WBS000089", "WBS000186", 
    "WBS000217", "WBS000073", "WBS000073", "WBS000073", "WBS000091", 
    "WBS000186", "WBS000222", "WBS000073", "WBS000146", "WBS000271", 
    "WBS000091", "WBS000043", "WBS000172", "WBS000073", "WBS000073", 
    "WBS000073", "WBS000292", "WBS000102", "WBS000073", "WBS000096", 
    "WBS000037", "WBS000165", "WBS000091", "WBS000318", "WBS000201", 
    "WBS000263", "WBS000125", "WBS000073", "WBS000307", "WBS000091", 
    "WBS000187", "WBS000095", "WBS000152"), impact = c(4, 4, 
    4, 4, 4, 3, 4, 3, 5, 4, 4, 5, 5, 5, 5, 3, 4, 4, 4, 5, 4, 
    4, 4, 4, 5, 2, 5, 5, 5, 5, 4, 4, 4, 4, 3, 4, 4, 3, 5, 5, 
    4, 3, 5, 5, 4, 4, 3, 5, 5, 5), urgency = c(4, 4, 4, 4, 4, 
    3, 4, 3, 5, 4, 4, 5, 5, 5, 5, 3, 4, 4, 4, 5, 4, 4, 4, 4, 
    5, 2, 5, 5, 5, 5, 5, 4, 4, 4, 3, 4, 4, 3, 5, 5, 4, 3, 5, 
    5, 4, 4, 3, 5, 5, 5), priority = c(4, 4, 4, 4, 4, 3, 4, 3, 
    5, 4, 4, 5, 5, 5, 5, 3, 4, 4, 4, 5, 4, 4, 4, 4, 5, 2, 5, 
    5, 5, 5, 4, 4, 4, 4, 3, 4, 4, 3, 5, 5, 4, 3, 5, 5, 4, 4, 
    3, 5, 5, 5), category = c("incident", "incident", "incident", 
    "incident", "incident", "incident", "incident", "incident", 
    "incident", "incident", "incident", "request for information", 
    "request for information", "incident", "incident", "incident", 
    "incident", "incident", "incident", "incident", "incident", 
    "incident", "incident", "incident", "incident", "incident", 
    "request for information", "incident", "incident", "incident", 
    "incident", "incident", "incident", "incident", "incident", 
    "incident", "incident", "incident", "request for information", 
    "incident", "incident", "incident", "request for information", 
    "incident", "incident", "incident", "incident", "incident", 
    "request for information", "incident"), km_number = c("KM0000846", 
    "KM0001401", "KM0000147", "KM0000751", "KM0001416", "KM0001012", 
    "KM0000819", "KM0000940", "KM0001925", "KM0002059", "KM0001153", 
    "KM0000225", "KM0001124", "KM0002267", "KM0000075", "KM0001764", 
    "KM0000508", "KM0001145", "KM0002015", "KM0001079", "KM0002043", 
    "KM0000316", "KM0000508", "KM0000347", "KM0001986", "KM0000274", 
    "KM0002243", "KM0001444", "KM0001763", "KM0001979", "KM0000711", 
    "KM0002360", "KM0002285", "KM0000918", "KM0000685", "KM0002041", 
    "KM0001026", "KM0000286", "KM0001445", "KM0001446", "KM0000839", 
    "KM0000895", "KM0001825", "KM0001158", "KM0000720", "KM0001859", 
    "KM0002122", "KM0001447", "KM0000225", "KM0001792"), alert_status = c("closed", 
    "closed", "closed", "closed", "closed", "closed", "closed", 
    "closed", "closed", "closed", "closed", "closed", "closed", 
    "closed", "closed", "closed", "closed", "closed", "closed", 
    "closed", "closed", "closed", "closed", "closed", "closed", 
    "closed", "closed", "closed", "closed", "closed", "closed", 
    "closed", "closed", "closed", "closed", "closed", "closed", 
    "closed", "closed", "closed", "closed", "closed", "closed", 
    "closed", "closed", "closed", "closed", "closed", "closed", 
    "closed"), number_reassignments = c(2, 2, 3, 0, 4, 0, 0, 
    0, 0, 1, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 
    2, 1, 0, 3, 3, 0, 0, 1, 4, 0, 0, 0, 2, 2, 0, 1, 3, 1, 0, 
    0, 0, 0, 0, 6), reopen_time = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), resolved_time = c("13/12/2013 14:17:31", "30/10/2013 08:00:58", 
    "26/03/2014 14:43:19", "12/12/2013 12:03:12", "21/11/2013 13:40:24", 
    "18/03/2014 11:28:56", "11/03/2014 10:12:04", "23/01/2014 10:02:58", 
    "15/10/2013 12:30:38", "09/01/2014 17:31:43", "05/12/2013 11:32:48", 
    "14/03/2014 08:47:13", "26/11/2013 15:37:37", "05/03/2014 12:58:14", 
    "12/12/2013 15:56:33", "14/01/2014 13:59:14", "06/12/2013 16:38:09", 
    "22/10/2013 11:00:28", "05/12/2013 08:20:12", "09/01/2014 13:07:47", 
    "05/11/2013 10:10:36", "14/02/2014 08:59:42", "02/01/2014 07:36:01", 
    "21/11/2013 09:52:25", "20/03/2014 15:40:04", NA, "10/02/2014 09:14:37", 
    "14/10/2013 08:01:54", "11/11/2013 14:30:20", "16/02/2014 21:59:53", 
    "15/01/2014 09:54:52", "19/03/2014 15:25:51", "10/02/2014 09:14:02", 
    NA, "19/11/2013 12:49:27", "23/01/2014 15:15:36", "27/01/2014 16:07:05", 
    "19/12/2013 17:20:15", "14/10/2013 12:26:59", NA, "13/11/2013 08:54:17", 
    "24/02/2014 08:08:30", "13/11/2013 15:22:50", "11/02/2014 13:10:16", 
    "17/03/2014 12:15:09", "04/12/2013 09:49:58", "12/11/2013 17:58:49", 
    "07/11/2013 08:41:58", "31/12/2013 12:59:34", "06/11/2013 11:58:44"
    ), handle_time_hours = c(3.001944444, 7.505555556, 44.99861111, 
    1.095277778, 15.21805556, 1.585277778, 0.393888889, 0.165277778, 
    8.5425, 6.087222222, 28.00944444, 0.576111111, 4.375277778, 
    2.139722222, 1.218888889, 2.458888889, 0.944444444, 8.175277778, 
    3.334444444, 27.51444444, 1.238055556, 33.52583333, 2.881944444, 
    0.035555556, 69.00805556, 7.883333333, 4.407777778, 16.04666667, 
    9.910277778, 35.31194444, 19.99666667, 1.332777778, 0.069166667, 
    12.56111111, 4.091944444, 1.355277778, 4.868888889, 0.879444444, 
    52.23944444, 25.11583333, 4.096944444, 0.671666667, 9.864722222, 
    9.2725, 0.424722222, 0.691944444, 6.216944444, 14.69944444, 
    2.266111111, 74.90166667), closure_code = c("Other", "Other", 
    "Software", "Other", "Software", "Software", "Other", "Software", 
    "Hardware", "Software", "User manual not used", "User error", 
    "Other", "User manual not used", "Software", "Software", 
    "Other", "Software", "Other", "Unknown", "Software", "Hardware", 
    "Software", "Other", "Other", NA, "Other", "Hardware", "Other", 
    "Unknown", "No error - works as designed", "User error", 
    "Data", "Other", "Software", "Software", "Software", "Other", 
    "Other", "Hardware", "Software", "No error - works as designed", 
    "Other", "Other", "No error - works as designed", "User error", 
    "Software", "Hardware", "Data", "Software"), number_related_interactions = c(1, 
    1, 2, 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, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), related_interaction = c("SD0063409", 
    "SD0024255", "#MULTIVALUE", "SD0062325", "SD0043046", "SD0136443", 
    "SD0130798", "SD0092388", "SD0012773", "SD0080457", "SD0053385", 
    "SD0133832", "SD0048144", "SD0126523", "SD0062696", "SD0083973", 
    "SD0058118", "SD0018087", "SD0055638", "SD0077609", "SD0030088", 
    "SD0107563", "SD0074306", "SD0044123", "SD0131936", "SD0129383", 
    "SD0106998", "SD0008168", "SD0034488", "SD0105623", "#MULTIVALUE", 
    "SD0138015", "SD0107361", "SD0117785", "SD0041819", "SD0092703", 
    "SD0094817", "SD0068847", "SD0006866", "SD0121426", "SD0036780", 
    "SD0118589", "SD0036886", "SD0107549", "SD0135508", "SD0054981", 
    "SD0030010", "SD0030880", "SD0073630", "SD0023004"), number_related_incidents = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), number_related_changes = c(NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA), related_change = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "C00003013", 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    ci_name_c_by = c("WBA000144", "WBA000144", "SBA000142", "SUB000456", 
    "WBA000145", "DTA000056", "WBA000058", "SSW000101", "PRN000140", 
    "SBA000659", "WBA000133", "SUB000113", "SAP000005", "SUB000426", 
    "WBA000011", "DTA000302", "SBA000010", "DTA000027", "SUB000456", 
    "WBA000103", "WBA000058", "LAP000570", "SBA000010", "DTA000035", 
    "SBA000458", "#N/B", "SAP000005", "LAP000446", "SBA000782", 
    "WBA000148", "WBA000133", "SBA000461", "WBA000133", "APP000004", 
    "WSR001354", "SUB000456", "DTA000016", "WBA000044", "DSK000356", 
    "DSK000258", "WBA000144", "SSW000101", "SBA000607", "SUB000443", 
    "WBA000018", "SBA000427", "LAP000018", "LAP000055", "SUB000113", 
    "SBA000074"), ci_type_c_by = c("application", "application", 
    "application", "subapplication", "application", "application", 
    "application", "software", "officeelectronics", "application", 
    "application", "subapplication", "application", "subapplication", 
    "application", "application", "application", "application", 
    "subapplication", "application", "application", "computer", 
    "application", "application", "application", "#N/B", "application", 
    "computer", "application", "application", "application", 
    "application", "application", "application", "computer", 
    "subapplication", "application", "application", "computer", 
    "computer", "application", "software", "application", "subapplication", 
    "application", "application", "computer", "computer", "subapplication", 
    "application"), ci_subtype_c_by = c("Web Based Application", 
    "Web Based Application", "Server Based Application", "Web Based Application", 
    "Web Based Application", "Desktop Application", "Web Based Application", 
    "System Software", "Printer", "Server Based Application", 
    "Web Based Application", "Server Based Application", "SAP", 
    "Web Based Application", "Web Based Application", "Desktop Application", 
    "Server Based Application", "Desktop Application", "Web Based Application", 
    "Web Based Application", "Web Based Application", "Laptop", 
    "Server Based Application", "Desktop Application", "Server Based Application", 
    "#N/B", "SAP", "Laptop", "Server Based Application", "Web Based Application", 
    "Web Based Application", "Server Based Application", "Web Based Application", 
    "Citrix", "Windows Server", "Web Based Application", "Desktop Application", 
    "Web Based Application", "Desktop", "Desktop", "Web Based Application", 
    "System Software", "Server Based Application", "Web Based Application", 
    "Web Based Application", "Server Based Application", "Laptop", 
    "Laptop", "Server Based Application", "Server Based Application"
    ), service_comp_wbs_c_by = c("WBS000318", "WBS000318", "WBS000199", 
    "WBS000073", "WBS000318", "WBS000091", "WBS000073", "#N/B", 
    "WBS000096", "WBS000255", "WBS000073", "WBS000095", "WBS000271", 
    "WBS000073", "WBS000152", "WBS000089", "WBS000186", "WBS000217", 
    "WBS000073", "WBS000073", "WBS000073", "WBS000091", "WBS000186", 
    "WBS000222", "WBS000073", "#N/B", "WBS000271", "WBS000091", 
    "WBS000043", "WBS000172", "WBS000073", "WBS000073", "WBS000073", 
    "WBS000292", "WBS000102", "WBS000073", "WBS000096", "WBS000037", 
    "WBS000187", "WBS000091", "WBS000318", "#N/B", "WBS000263", 
    "WBS000125", "WBS000073", "WBS000307", "WBS000091", "WBS000187", 
    "WBS000095", "WBS000152"), is_outlier_handle_time_hours_total = c(FALSE, 
    FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, TRUE)), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))

CodePudding user response:

Here is a way.
I have used the built-in data set iris because the problem seems to be solvable following the lines below.

  • Create a function of the multiplier and columns to do the calculations;
  • create a function of the multiplier and data set (the data.frame or tibble) to group, summarise and create the computed column or columns;
  • create a vector of multipliers m;
  • call the aggregation function with map.
suppressPackageStartupMessages({
  library(dplyr)
  library(purrr)
  library(ggplot2)
})

fun_calc <- function(mult, x) {
  x/sum(x) * mult
}
fun_mult <- function(mult, data) {
  data %>%
    group_by(Species) %>%
    summarise(total = sum(Sepal.Length)) %>%
    mutate(ahmdal_A = fun_calc(mult, total)) %>%
    arrange(desc(ahmdal_A))
}

m <- seq(1.2, 5, by = 0.2)

m %>%
  map_dfr(fun_mult, .id = "multiplier", data = iris) %>%
  mutate(multiplier = as.integer(multiplier),
         multiplier = m[multiplier])
#> # A tibble: 60 × 4
#>    multiplier Species    total ahmdal_A
#>         <dbl> <fct>      <dbl>    <dbl>
#>  1        1.2 virginica   329.    0.451
#>  2        1.2 versicolor  297.    0.406
#>  3        1.2 setosa      250.    0.343
#>  4        1.4 virginica   329.    0.526
#>  5        1.4 versicolor  297.    0.474
#>  6        1.4 setosa      250.    0.400
#>  7        1.6 virginica   329.    0.601
#>  8        1.6 versicolor  297.    0.542
#>  9        1.6 setosa      250.    0.457
#> 10        1.8 virginica   329.    0.676
#> # … with 50 more rows

Created on 2022-12-20 with reprex v2.0.2


To plot pipe the output above to ggplot.

m %>%
  map_dfr(fun_mult, .id = "multiplier", data = iris) %>%
  mutate(multiplier = as.integer(multiplier),
         multiplier = m[multiplier]) %>%
  ggplot(aes(multiplier, ahmdal_A, color = Species))  
  geom_line()  
  geom_point()  
  theme_bw()

Created on 2022-12-20 with reprex v2.0.2

  •  Tags:  
  • r
  • Related