# Filter table rows for regression

I have a table containing a flag and x and y values. I'd like to have a regression for all rows with 1 in the first column. How can I do this?

\documentclass{article}
\usepackage{array}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5.1}
\usepackage{pgfplotstable}
\begin{document}
Exmpl  a   v
1      0   0
1      1   1
1      2   1
1      3   4
2      0   -0
2      1   -1
2      2   -1
2      3   -4
} \data
\begin{tikzpicture}
\begin{axis} % [legend pos=outer north east]
\addplot table [x=a, y={create col/linear regression={y=v}}] {\data};
\end{axis}
\end{tikzpicture}
\end{document}


I think, the pgfplots filters don't apply here, since I guess, pgfplots only gets the x and y values. It would be great, if there is a possibility to create a filtered table and do the regression and plotting stuff from there.

I think I have almost the solution with the help of two questions: Create a contingency table using pgfplotstable and Accessing individual table elements with pgfplots? The idea is, to transpose the matrix, create a new matrix, loop over all columns and copy the columns, if the Exmpl - value is 1.

\documentclass{article}
\usepackage{array}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5.1}
\usepackage{pgfplotstable}
\usepackage{ifthen}

\newcommand{\pgfplotstablefilterrows}[3]
{
\pgfplotstablegetrowsof{#1}
\pgfmathsetmacro{\NumOfRows}{\pgfplotsretval}
\pgfmathsetmacro{\MaxRow}{\NumOfRows-1}
\pgfplotstablegetcolsof{#1}
\pgfmathsetmacro{\NumOfCols}{\pgfplotsretval}

\pgfplotstabletranspose{\TransposedData}{#1}
\pgfplotstableset{create on use/TransposedHead/.style={copy column from table={\TransposedData}{[index]0}}}
\pgfplotsforeachungrouped \pgfplotstablerowindex in {0,1,...,\MaxRow}{ % Row loop
#3
}
\pgfplotstableclear{\TransposedData}
\pgfplotstableclear{\TransposedFilteredData}
}

\begin{document}
Exmpl  a   v
1      0   0
1      1   1
1      2   1
1      3   4
2      0   -0
2      1   -1
2      2   -1
2      3   -4
} \data

\pgfplotstablefilterrows{\data}{\FilteredData}
{
\pgfplotstablegetelem{\pgfplotstablerowindex}{[index]0}\of\data
\ifthenelse{\equal{\pgfplotsretval}{1}}
{
\pgfplotstablecreatecol[copy column from table={\TransposedData}{\pgfplotstablerowindex}]{\pgfplotstablerowindex}{\TransposedFilteredData}
}
{}
}

\begin{tikzpicture}
\begin{axis} % [legend pos=outer north east]