簡體   English   中英

在 R 中創建一個表來總結結果

[英]Create a table in R to summarize outcome

我有一個包含以下數據的數據框( movement ): displVolumCat2perc_DVHT_99 , motion

“displ”包含:0.5mm、1.0mm、1.5mm “perc_DVHT_99 包含:我想總結的變量 bij 中位數(1st Qu.、3rd Qu.)“volumCat2”包含:a、b1、b2、c、d , e“運動”包含:旋轉平移

我想創建一個匯總表,如下所示:

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"))

如何在 R 中創建它?

謝謝

使用答案,我能夠創建 4 個看起來像的表在此處輸入圖像描述 但是我怎樣才能改變平移和旋轉的position。 我希望翻譯的結果高於旋轉的結果。 以及如何將(1st Qu.,3rd QU.)放在中值下方?

謝謝

以下是中位數的答案:

# 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)

如果我們現在想為第 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