简体   繁体   中英

Create a table in R to summarize outcome

I have a data frame (setXY) containing the following data: displ , perc_DVHT_99 , VolumCat2 , movement

"displ" contains: 0.5mm, 1.0mm, 1.5mm "perc_DVHT_99 contains: the variable that I would like to summarize bij median(1st Qu., 3rd Qu.) "volumCat2"contains: a, b1, b2, c, d, e "movement"contains: rotation translation

I would like to create a summary table which looks like:

dput(setXY[1:10,])

structure(list(displ = c("0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm", 
"0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm"), 
    perc_DVH = c(99.169574073565, 98.3998642978761, 99.3452539098338, 
    98.3301531618343, 97.8633859305831, 97.572227542085, 99.3287258697977, 
    99.3033293087417, 95.287598273786, 97.0386976259169), VolumCat2 = c("e", 
    "e", "e", "e", "b1", "b1", "b1", "b1", "b1", "b1"), movement = c("t", 
    "t", "t", "t", "t", "t", "t", "t", "t", "t")), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

How do I create this in R?

Thanks

Using the answer I was able to create 4 tables looking like在此处输入图像描述 But how can I shift position of translation and rotation. I would like to have the result of translation above those of rotation. And how can I place the (1st Qu., 3rd QU.) underneath the median values?

Thanks

Here is the answer for the median:

# load some helper packages
library(tidyverse)

# your dataframe
df <- structure(list(displ = c("0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm", 
"0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm"), 
    perc_DVH = c(99.169574073565, 98.3998642978761, 99.3452539098338, 
    98.3301531618343, 97.8633859305831, 97.572227542085, 99.3287258697977, 
    99.3033293087417, 95.287598273786, 97.0386976259169), VolumCat2 = c("e", 
    "e", "e", "e", "b1", "b1", "b1", "b1", "b1", "b1"), movement = c("t", 
    "t", "t", "t", "t", "t", "t", "t", "t", "t")), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

# now, we take the dataframe and then...
df %>%
    # ...for each combination of movement, displ and VolumCat2...
    group_by(movement, displ, VolumCat2) %>%
    # we calculate the median which gives us a long dataframe with only one column for the medians
    summarise(perc_median = median(perc_DVH)) %>%
    # and now we leave movement and displ as rows, but put VolumCat2 into rows
    pivot_wider(id_cols = c("movement", "displ")
                , names_from = VolumCat2
                , values_from = perc_median)

If we now want to create the same for the 1st and 3rd quartile, we just need to replace median(perc_DVH) by quantile(perc_DVH, probs = 0.25) and quantile(perc_DVH, probs = 0.75) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM