Liner regression between 2 variables but only in selected lines.

Take a look at this related thread, this shows how to perform linear regressions over nested subgroups of your data frame

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.