如何计算列联表?

如何计算列联表?

我正在尝试计算两个离散变量name和的列联表location。缺失级别应为 0,而每个单元格应为每个名称-位置组合的计数。我最初的方法是创建计数器,然后将它们格式化为表格,但我不知道如何嵌套 if 语句。另外,我假设一定有一个数据工具方法?

\documentclass{article}

\usepackage{xstring}
\usepackage{datatool}

\begin{filecontents*}{fruit.csv}
name,location
Apple,NY
Apple,LA
Orange,LA
Banana,LA
Banana,NY
Banana,NY
\end{filecontents*}

\DTLloaddb{fruit}{fruit.csv}
\newcounter{applyNY}

\begin{document}


\DTLforeach*{fruit}{\name=name,\loc=location}{%
    \IfStrEqCase{\name}{%
     {Apple}{\IfStrEqCase{\loc}{NY}
        {\stepcounter{applyNY}}}%
    }%
}


There are \theapplyNY 

\end{document}

按照我的方法输出的是

有2个

当我寻找 1 时(名称 = Apple 且位置 = NY)

答案1

您缺少一副牙套:

\documentclass{article}

\usepackage{xstring}
\usepackage{datatool}

\begin{filecontents*}{fruit.csv}
name,location
Apple,NY
Apple,LA
Orange,LA
Banana,LA
Banana,NY
Banana,NY
\end{filecontents*}

\DTLloaddb{fruit}{fruit.csv}
\newcounter{applyNY}

\begin{document}


\DTLforeach*{fruit}{\name=name,\loc=location}{%
  \IfStrEqCase{\name}{%
    {Apple}{%
      \IfStrEqCase{\loc}{%<--- This was missing
        {NY}{\stepcounter{applyNY}}%
      }%<--- This was missing
    }%
}}

There are \theapplyNY 

\end{document}

检查一下,你会发现打印的数字是1。

这是一个相当通用的版本,您可以在加载数据库后提取值并打印列联表本身。

\begin{filecontents*}{fruit.csv}
name,location
Apple,NY
Apple,LA
Orange,LA
Banana,LA
Banana,NY
Banana,NY
\end{filecontents*}

\documentclass{article}

\usepackage{datatool}
\usepackage{xparse}
\usepackage{booktabs}

\ExplSyntaxOn

\NewDocumentCommand{\loadcontingencytable}{mmm}
 {% #1 = DB name
  % #2 = first col name
  % #3 = second col name
  \dambo_ct_load:nnn { #1 } { #2 } { #3 }
 }

\NewDocumentCommand{\getcontingencytablevalue}{mmm}
 {
  \dambo_ct_getvalue:nnn { #1 } { #2 } { #3 }
 }

\NewDocumentCommand{\showcontingencytable}{m}
 {
  \dambo_ct_show:n { #1 }
 }

\tl_new:N \l__dambo_ct_row_tl
\tl_new:N \l__dambo_ct_col_tl
\tl_new:N \l__dambo_ct_rowcol_tl
\tl_new:N \l__dambo_ct_body_tl
\int_new:N \l__dambo_ct_cols_int
\seq_new:N \l__dambo_ct_rows_tl
\seq_new:N \l__dambo_ct_cols_tl
\prop_new:N \l__dambo_ct_table_prop

\cs_generate_variant:Nn \prop_put:Nnn { NVx }
\cs_generate_variant:Nn \prop_item:Nn { NV, cx }

\cs_new_protected:Nn \dambo_ct_load:nnn
 {
  \seq_new:c { g_dambo_ct_rows_#1_seq }
  \seq_new:c { g_dambo_ct_cols_#1_seq }
  \prop_new:c { g_dambo_ct_table_#1_prop }
  \seq_clear:N \l__dambo_ct_rows_seq
  \seq_clear:N \l__dambo_ct_cols_seq
  \prop_clear:N \l__dambo_ct_table_prop

  \DTLforeach*{#1}
   {
    \l__dambo_ct_row_tl = #2,\l__dambo_ct_col_tl = #3
   }
   {
    \seq_put_right:NV \l__dambo_ct_rows_seq \l__dambo_ct_row_tl
    \seq_put_right:NV \l__dambo_ct_cols_seq \l__dambo_ct_col_tl
    \tl_set:Nx \l__dambo_ct_rowcol_tl
     {
      \exp_not:V \l__dambo_ct_row_tl
      \exp_not:V \l__dambo_ct_col_tl
     }
    \prop_put:NVx \l__dambo_ct_table_prop \l__dambo_ct_rowcol_tl
     {
      \int_eval:n
       {
        0\prop_item:NV \l__dambo_ct_table_prop \l__dambo_ct_rowcol_tl + 1
       }
     }
   }
  \seq_remove_duplicates:N \l__dambo_ct_rows_seq
  \seq_remove_duplicates:N \l__dambo_ct_cols_seq
  \seq_set_eq:cN { g_dambo_ct_rows_#1_seq } \l__dambo_ct_rows_seq
  \seq_set_eq:cN { g_dambo_ct_cols_#1_seq } \l__dambo_ct_cols_seq
  \prop_set_eq:cN { g_dambo_ct_table_#1_prop } \l__dambo_ct_table_prop
 }

\cs_new_protected:Nn \dambo_ct_getvalue:nnn
 {
  \prop_item:cn { g_dambo_ct_table_#1_prop } { #2#3 }
 }

\cs_new_protected:Nn \dambo_ct_show:n
 {
  \int_set:Nn \l__dambo_ct_cols_int { \seq_count:c { g_dambo_ct_cols_#1_seq } }
  \tl_set:Nx \l__dambo_ct_body_tl
   {
    & \seq_use:cn { g_dambo_ct_cols_#1_seq } { & }
    \exp_not:n { \\ \midrule }
   }
  \seq_map_inline:cn { g_dambo_ct_rows_#1_seq }
   {
    \tl_put_right:Nn \l__dambo_ct_body_tl { ##1 }
    \int_step_inline:nn { \l__dambo_ct_cols_int }
     {
      \tl_put_right:Nx \l__dambo_ct_body_tl
       {
        &
        \prop_item:cx { g_dambo_ct_table_#1_prop }
         { ##1 \seq_item:cn { g_dambo_ct_cols_#1_seq } { ####1 } }
       }
     }
    \tl_put_right:Nn \l__dambo_ct_body_tl { \\ }
   }
  \begin{tabular}{ l *{\l__dambo_ct_cols_int}{c} }
  \toprule
  \tl_use:N \l__dambo_ct_body_tl
  \bottomrule
  \end{tabular}
 }

\ExplSyntaxOff

\DTLloaddb{fruit}{fruit.csv}
\loadcontingencytable{fruit}{name}{location}

\begin{document}

Apple, NY: \getcontingencytablevalue{fruit}{Apple}{NY}

Banana, NY: \getcontingencytablevalue{fruit}{Banana}{NY}

\bigskip

\showcontingencytable{fruit}

\end{document}

在此处输入图片描述

相关内容