-1

the code below shows me extracting certain values for 1 parameter in my data frame (Calcium), but I want to be able to do this for all of the parameters/rows in my data frame. There are multiple rows for Calcium, which is why I took the median value. How can I create a loop that does this for the other drug substance parameters?

Cal_limits=ag_limits_5 %>% filter(PARAMETER=="Drug Substance.Calcium")

lcl <- median(Cal_limits$LCL, na.rm = TRUE)
ucl <- median(Cal_limits$UCL, na.rm = TRUE)
lsl <- median(Cal_limits$LSL_1, na.rm = TRUE)
usl <- median(Cal_limits$USL_1, na.rm = TRUE)
cl <- median(Cal_limits$TARGET_MEAN, na.rm = TRUE)
stdev <- median(Cal_limits$TARGET_STDEV, na.rm = TRUE)
sigabove <- ucl + stdev #3.219 #(UCL + sd (3.11+0.107))
sigbelow <- lcl - stdev#2.363 #(LCL - sd (2.47-0.107))

Snapshot showing that there are multiple rows dedicated to one parameter, the columns not pictured have confidential information but include the values I am looking to extract

Edit: I am creating an RShiny app, so I am not sure if I will need to incorporate a reactive function

r2evans
  • 108,754
  • 5
  • 72
  • 122
Natasha H
  • 47
  • 5
  • Can you explain what your expected result is?? Another df? A single value for each column in your df? – Daniel Jul 02 '21 at 19:56
  • 1
    FYI, shiny's reactivity has nothing to do with your question of how to summarize your data. Once you learn how to do it on the R console, you can then apply that to whatever reactive components you may need in a shiny app. – r2evans Jul 02 '21 at 20:05

1 Answers1

1

Using mtcars, you can do

aggregate(. ~ cyl, data = mtcars, FUN = median)
#   cyl  mpg  disp    hp  drat    wt   qsec vs am gear carb
# 1   4 26.0 108.0  91.0 4.080 2.200 18.900  1  1    4  2.0
# 2   6 19.7 167.6 110.0 3.900 3.215 18.300  1  0    4  4.0
# 3   8 15.2 350.5 192.5 3.115 3.755 17.175  0  0    3  3.5

which provides the median for each of the variables (. means "all others") for each of the levels of cyl. I'm going to guess that this would apply to your data as

aggregate(. ~ PARAMETER, data = ag_limits_5, FUN = median)

If you have more columns than you want to reduce, then you can specify them manually with

aggregate(LCL + UCL + LSL_1 + USL_1 + TARGET_MEAN + TARGET_STDDEV ~ PARAMETER,
          data = ag_limits_5, FUN = median)

and I think you'll get output something like

#                PARAMETER  LCL  UCL  LSL_1  USL_1  TARGET_MEAN  TARGET_STDDEV
# 1 Drug Substance.Calcium  1.1  1.2    1.3    1.4          ...
# 2  Drug Substance.Copper  ...

(with real numbers, I'm just showing structure there).

Since it appears that you're using dplyr, you can do it this way, too:

mtcars %>%
  group_by(cyl) %>%
  summarize(across(everything(), ~ median(., na.rm = TRUE)))
# # A tibble: 3 x 11
#     cyl   mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb
#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     4  26    108    91   4.08  2.2   18.9     1     1     4   2  
# 2     6  19.7  168.  110   3.9   3.22  18.3     1     0     4   4  
# 3     8  15.2  350.  192.  3.12  3.76  17.2     0     0     3   3.5

which for you might be

ag_limits_5 %>%
  group_by(PARAMETER) %>%
  summarize(across(everything(), ~ median(., na.rm = TRUE)))
r2evans
  • 108,754
  • 5
  • 72
  • 122
  • Thank you, that worked! Would you know how to incorporate this into an RShiny server function so that the values are automatically extracted depending on which parameter the individual chooses? – Natasha H Jul 02 '21 at 20:13
  • 1
    As I said in my previous comment, doing this aggregation has nothing to do with shiny. How to incorporate this into a shiny app depends on your data, how your data is available in your app, how you intend to show it, etc. – r2evans Jul 02 '21 at 20:17
  • 1
    I suggest that when you're satisfied with this step in your process, then please [accept](https://stackoverflow.com/help/someone-answers) the answer and ask a new question focusing on shiny itself. For that I suggest including enough of your shiny code (but not too much!) so that somebody can take the code, try it, adapt/fix it, and give it back. (If you are not comfortable/familiar with MWE, please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info.) Thanks! – r2evans Jul 02 '21 at 20:19