0

I have 30 Patients with their 100 clinical data such as weight,BMI, waist etc and i want to take mean and SD for all the patients based on their Disease status For example my data set looks like

Patient_id   DateOfBirth       Sex     Weight1   Bmi1   Wasit1  Disease
204065       25-06-1995       Female    113.8    41.3   105.8   0
200214       09-12-1990       Female      90     35.6   108     1
191633       14-09-1971         Male    128.4    47     150     1
186156       22-09-1967         Male    157.3    51.4   145.6   0

and i want output based on their disease status like

Disease weight1Mean  Weight1SD      BMI1Mean    BMI1SD     Waist1Mean  WaistSD  
  0        135           30.7         46.3       7.14       125.7       28.1
  1        109           27.1         41.3       8.06       129         29.7
markus
  • 25,843
  • 5
  • 39
  • 58
Katrix_02
  • 83
  • 6
  • Possible dupe: [Apply several summary functions on several variables by group in one call](https://stackoverflow.com/questions/12064202/apply-several-summary-functions-on-several-variables-by-group-in-one-call) – markus Mar 24 '20 at 16:38

2 Answers2

0
your_df %>%
groupy_by(Disease) %>%
summarize(Weight1Mean = mean(Weight1),
Weight1SD = sd(Weight1
#Repeat for the rest of variables to sumamrize
)

You can also use summarize_at in place of summarize:

#... %>%
summarize_at(vars(Weight1, BMI1, Waist1), list(Mean = mean, SD = sd))

Or summarize_if:

#... %>%
summarize_if(is.numeric, list(Mean = mean, SD = sd))

If you have numeric variables you want to exclude from summarization, you can recode them as factors or drop them with select.

RaV
  • 617
  • 1
  • 5
  • 11
  • 1
    Thanks for the code but i need all variables Mean and SD and i cannot specify for each due to 1000 of columns in the dataset any recommendation to get all mean and SD in a single functional output – Katrix_02 Mar 24 '20 at 17:10
  • @Matrix_32 sorry, I didn't notice 100 variables part: in that case use `summarize_if(is.numeric, list(Mean = mean, SD = sd))`. If you have some numeric values, that you want to exclude from `summarize`, recode them as factors for example. – RaV Mar 24 '20 at 17:36
0

We can use data.table

 library(data.table)
 setDT(df1)[, .(Weight1Mean = mean(Weight1), Weight1SD = sd(Weight1)), Disease]
akrun
  • 874,273
  • 37
  • 540
  • 662