在R中创建频率分析结果表

在R中创建频率分析结果表

本文介绍了在R中创建频率分析结果表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要基于某种模板创建某种类型的表。

I need to create a table of a certain type and based on a certain template.

这是我的数据:

df = structure(list(group = c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L),
                    degree = structure(c(1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L),
                                       .Label = c("Mild severity", "Moderate severity", "Severe severity"),
                                       class = "factor")),
               .Names = c("group", "degree"),
               class = "data.frame",
               row.names = c(NA, -10L))

我进行了交叉分析:

table(df$degree,df$group)

                    1 2 3
  Mild severity     3 3 2
  Moderate severity 0 0 1
  Severe severity   0 0 1

但是我需要将结果格式化为此te mplate:
[![在此处输入图片描述] [1]] [1]

but I need the results to be formatted in this template:[![enter image description here][1]][1]

如何创建具有这种结构的表?

How can I create a table with this structure?

完整dput()(42个观测点)

full dput() (42 obs.)

df = structure(list(Study.Subject.ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 5L, 7L, 8L, 9L, 1L, 2L, 3L, 5L, 8L, 2L, 3L, 5L, 8L, 2L, 3L, 5L, 8L, 2L, 3L, 5L, 8L, 3L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L),
                                                 .Label = c("01-06-104", "01-09-108", "01-15-201", "01-16-202", "01-18-204", "01-27-301", "01-28-302", "01-33-305", "01-42-310"),
                                                 class = "factor"),
                    group = c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 1L, 2L, 2L, 3L, 1L, 2L, 2L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
                    Degree.of.severity = structure(c(2L, 2L, 2L, 2L, 2L, 4L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L),
                                                   .Label = c("Life-threatening or disabling", "Mild severity", "Moderate severity", "Severe severity"),
                                                   class = "factor")),
              .Names = c("Study.Subject.ID", "group", "Degree.of.severity"),
              class = "data.frame",
              row.names = c(NA, -42L))

有一个主题概念,并且有很多副作用。
一个人可以有多种副作用。
副作用可能是

There is a concept of the subject, and there is concept a number of side effects.One person can have several side effects.The side effect can be

severity
Moderate
Severe

我必须计算出有多少人按组分开或有这种副作用,
以及该组有多少副作用?

I have to count how many people separated by group have this or that side effect,and how many side effects are in this group?

IE在第一组中,我们有9个观测值,但是有两个独特的人。

I.E. In the first group we have 9 observations, but there are two unique people.

01-06-104
01-09-108

但总数轻度严重程度为7。
因此,只有两个人的副作用为轻微严重度(X),总计数轻微严重度为7(Y)。
患者总数为42,因此要计算百分比,我们必须除以42(2/42)= 4,7

but total count Mild severity is 7.So only two people have side effects of Mild severity (X) and total count Mild severity is 7 (Y).Total count of patients is 42, so to calculate percentage we must divide by 42 (2/42)=4,7

这就是为什么我希望输出为:

That's why I expected the output to be:

    degree       group1           group2         group3
                  X (%)Y          X (%)Y         X (%) Y

    Mild severity   2 (4,7%)7   3 (7,1%)13   3(7,1%)    12
    Moderato        1 (2,3%)1   0(0,0%%)0    2(4,7%)    6
    Severe severity 0(0,0%%)0   0(0,0%%)0     1(2,3)    1


推荐答案

我必须承认我不清楚您要做什么。不幸的是,您的预期输出图像无济于事。

I have to admit that I'm not clear on what you're trying to do. Unfortunately your expected output image does not help.

我假设,您在问如何计算2向列联表,并同时显示计数和百分比(占总数)。这是 tidyverse 的可能性

library(tidyverse)
df %>%
    group_by(group, degree) %>%
    summarise(n = n(), perc = n() / nrow(.)) %>%
    mutate(entry = sprintf("%i (%3.2f%%)", n, perc * 100)) %>%
    select(-n, -perc) %>%
    spread(group, entry, fill = "0 (0.0%%)")
## A tibble: 3 x 4
#  degree            `1`        `2`        `3`
#  <fct>             <chr>      <chr>      <chr>
#1 Mild severity     3 (30.00%) 3 (30.00%) 2 (20.00%)
#2 Moderate severity 0 (0.0%%)  0 (0.0%%)  1 (10.00%)
#3 Severe severity   0 (0.0%%)  0 (0.0%%)  1 (10.00%)

这篇关于在R中创建频率分析结果表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:02