简体   繁体   English

在 R 中创建一个表来总结结果

[英]Create a table in R to summarize outcome

I have a data frame (setXY) containing the following data: displ , perc_DVHT_99 , VolumCat2 , movement我有一个包含以下数据的数据框( movement ): displVolumCat2perc_DVHT_99 , motion

"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 “displ”包含:0.5mm、1.0mm、1.5mm “perc_DVHT_99 包含:我想总结的变量 bij 中位数(1st Qu.、3rd Qu.)“volumCat2”包含:a、b1、b2、c、d , e“运动”包含:旋转平移

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?如何在 R 中创建它?

Thanks谢谢

Using the answer I was able to create 4 tables looking like使用答案,我能够创建 4 个看起来像的表在此处输入图像描述 But how can I shift position of translation and rotation.但是我怎样才能改变平移和旋转的position。 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?以及如何将(1st Qu.,3rd QU.)放在中值下方?

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) .如果我们现在想为第 1 和第 3 个四分位数创建相同的值,我们只需将median(perc_DVH)替换为quantile(perc_DVH, probs = 0.25)quantile(perc_DVH, probs = 0.75)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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