Convert frequency table into other format

Why my frequency table is showing wrong format? Frequency table is shown in picture.
Annotation 2020-08-04 090354

My code is -

library(psych)
#> Warning: package 'psych' was built under R version 4.0.2
library(vcd)
#> Warning: package 'vcd' was built under R version 4.0.2
#> Loading required package: grid
library(DescTools)
#> Warning: package 'DescTools' was built under R version 4.0.2
#> 
#> Attaching package: 'DescTools'
#> The following objects are masked from 'package:psych':
#> 
#>     AUC, ICC, SD
library(rcompanion)
#> Warning: package 'rcompanion' was built under R version 4.0.2
#> 
#> Attaching package: 'rcompanion'
#> The following object is masked from 'package:psych':
#> 
#>     phi
df <- read.csv("F:/jar_test.csv", stringsAsFactors = F)
colnames(df)[1] = "sample_name"
colnames(df)[2] = "weak"
colnames(df)[3] = "JAR"
colnames(df)[4] = "strong"
ftable(df)
#>                      strong 40 41 42
#> sample_name weak JAR                
#> 170         22   32          0  0  0
#>                  37          0  0  0
#>                  40          0  0  0
#>             24   32          0  0  0
#>                  37          0  1  0
#>                  40          0  0  0
#>             28   32          0  0  0
#>                  37          0  0  0
#>                  40          0  0  0
#> 896         22   32          0  0  0
#>                  37          0  0  0
#>                  40          1  0  0
#>             24   32          0  0  0
#>                  37          0  0  0
#>                  40          0  0  0
#>             28   32          0  0  0
#>                  37          0  0  0
#>                  40          0  0  0
#> 914         22   32          0  0  0
#>                  37          0  0  0
#>                  40          0  0  0
#>             24   32          0  0  0
#>                  37          0  0  0
#>                  40          0  0  0
#>             28   32          0  0  1
#>                  37          0  0  0
#>                  40          0  0  0

Created on 2020-08-04 by the reprex package (v0.3.0)

Hello,

Although you did provide code using the reprex package, you did not provide us with a sample of the original dataframe df. This means it's not possible to run your code or see what the input data looks like. Please provide a sample of the input, and what you would want to do with it (frequency of what).

If you need help building a good reprex, read this:

PJ

1 Like

@pieterjanvc, thanks for the help. Actually I want to run Cochran-Mantel-Haenszel test, and i found this page where procedure was mentioned:
https://rcompanion.org/handbook/H_06.html
So i tried to run this but i had problem with the table, like i have samples (170, 896 and 914) like, this
170 weak 24
170 jar 37
170 strong 41
896 weak 22
896 jar 40
896 strong 40
914 weak 28
914 jar 32
914 strong 42

squads <- tibble::tribble(
  ~Product, ~`1`, ~`2`, ~`3`,
      170L,  24L,  37L,  41L,
      896L,  22L,  40L,  40L,
      914L,  28L,  32L,  42L
  )
head(squads)
#> # A tibble: 3 x 4
#>   Product   `1`   `2`   `3`
#>     <int> <int> <int> <int>
#> 1     170    24    37    41
#> 2     896    22    40    40
#> 3     914    28    32    42

Created on 2020-08-06 by the reprex package (v0.3.0)

I'm wondering if the dimensionality of the data is correct for the desired test.
mantelhaen.test requires a 3 dimensional structure.
But your data seems to have two dimensions ? i.e. Sample/Product as one dimension, and strong/weak/JAR as a second dimension. ?

@nirgrahamuk. Got it! I found about CMH test and i shall try to run as per shown on this link for 2 dimensional data:

https://cran.r-project.org/web/packages/samplesizeCMH/vignettes/samplesizeCMH-introduction.html#:~:text=The%20Cochran-Mantel-Haenszel%20test%20(CMH)%20is%20an,of%202%20×%202%20tables.

@nirgrahamuk. Can you please let me know what kind of data structure i need to run this test. I found this page, from where i am trying to run CMH test.
http://finzi.psych.upenn.edu/library/vcdExtra/html/CMHtest.html

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.