Creating a table for frequency analysis results in R

倾然丶 夕夏残阳落幕 提交于 2019-11-28 11:02:51

问题


I need create the table a certain type(template)

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

i conduct crosstab

table(df$degree,df$group)

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

but i need the results in this template [![enter image description here][1]][1]

How can i create table with this structure?

very important edit

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 in this group?

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

01-06-104
01-09-108

but total count Mild severity is 7. So only two person has side effect of Mild severity(X) and total count Mild severity is 7(Y). Total count of pacients are 42, so to calculate percentage we must divide by 42 (2/42)=4,7

that why expected output

    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

回答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.

I assume you are asking how to calculate a 2-way contingency table and show both counts and percentages (of total). Here is a tidyverse possibility

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



回答2:


you want the fractions, together with the total numbers? Try:

n=table(df$degree,df$group)
df=as.data.frame(cbind(n/colSums(n)*100,n))



回答3:


using base R:

a = transform(data.frame(table(df)),Freq = sprintf("%d (%3.2f%%)",Freq,prop.table(Freq)*100))
data.frame(t(unstack(a,Freq~degree)))
                          X1         X2         X3
Mild.severity     3 (30.00%) 3 (30.00%) 2 (20.00%)
Moderate.severity  0 (0.00%)  0 (0.00%) 1 (10.00%)
Severe.severity    0 (0.00%)  0 (0.00%) 1 (10.00%)


来源:https://stackoverflow.com/questions/51187293/creating-a-table-for-frequency-analysis-results-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!