Q: R code for linear model grouped by multiple columns I have a data.frame containing 5760 observations of 12 variables.  I have provided a subset containing only 32 obs of 8 variables for my example.
> (df1)
   GHGEventID    SmpDate Plot Ch SmpTime TrtIDs    ugN2O      PK
1           1 2019-06-04  101  1       0      8 7.644037 1 101 1
2           1 2019-06-04  101  1      10      8 7.420080 1 101 1
3           1 2019-06-04  101  1      20      8 7.618144 1 101 1
4           1 2019-06-04  101  1      30      8 7.612934 1 101 1
5           1 2019-06-04  101  2       0      8 7.754488 1 101 2
6           1 2019-06-04  101  2      10      8 7.979284 1 101 2
7           1 2019-06-04  101  2      20      8 7.919863 1 101 2
8           1 2019-06-04  101  2      30      8 7.719606 1 101 2
9           1 2019-06-04  202  1       0      5       NA 1 202 1
10          1 2019-06-04  202  1      10      5       NA 1 202 1
11          1 2019-06-04  202  1      20      5       NA 1 202 1
12          1 2019-06-04  202  1      30      5       NA 1 202 1
13          1 2019-06-04  202  2       0      5       NA 1 202 2
14          1 2019-06-04  202  2      10      5       NA 1 202 2
15          1 2019-06-04  202  2      20      5       NA 1 202 2
16          1 2019-06-04  202  2      30      5       NA 1 202 2
17          3 2019-06-19  101  1       0      8 8.704949 3 101 1
18          3 2019-06-19  101  1      10      8 8.897050 3 101 1
19          3 2019-06-19  101  1      20      8 8.977514 3 101 1
20          3 2019-06-19  101  1      30      8 9.307248 3 101 1
21          3 2019-06-19  101  2       0      8 9.067406 3 101 2
22          3 2019-06-19  101  2      10      8 9.194972 3 101 2
23          3 2019-06-19  101  2      20      8 8.988367 3 101 2
24          3 2019-06-19  101  2      30      8 9.378238 3 101 2
25          3 2019-06-19  202  1       0      5 8.792658 3 202 1
26          3 2019-06-19  202  1      10      5 8.988340 3 202 1
27          3 2019-06-19  202  1      20      5 9.124808 3 202 1
28          3 2019-06-19  202  1      30      5 8.977348 3 202 1
29          3 2019-06-19  202  2       0      5 8.711944 3 202 2
30          3 2019-06-19  202  2      10      5 8.981701 3 202 2
31          3 2019-06-19  202  2      20      5 9.358124 3 202 2
32          3 2019-06-19  202  2      30      5 9.381370 3 202 2
> str(df1)
'data.frame':   32 obs. of  8 variables:
 $ GHGEventID: num  1 1 1 1 1 1 1 1 1 1 ...
 $ SmpDate   : Factor w/ 30 levels "2019-06-04","2019-06-11",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Plot      : Factor w/ 24 levels "101","102","103",..: 1 1 1 1 1 1 1 1 10 10 ...
 $ Ch        : Factor w/ 2 levels "1","2": 1 1 1 1 2 2 2 2 1 1 ...
 $ SmpTime   : num  0 10 20 30 0 10 20 30 0 10 ...
 $ TrtIDs    : num  8 8 8 8 8 8 8 8 5 5 ...
 $ ugN2O     : num  7.64 7.42 7.62 7.61 7.75 ...
 $ PK        : chr  "1 101 1" "1 101 1" "1 101 1" "1 101 1" ...

I am looking for an approach to apply a linear model to the data based on the SmpDate, Plot, Ch so that I can use the coefficient for a secondary calculation.  An example would be like
y<-c(7.644037,7.420080,7.618144,7.612934)
x<-c(0,10,20,30)
LinMod<-lm(y~x,na.action=na.exclude)$coefficients[2]*60*24*10000/(0.144032*1000000)

Note that this is not a working example of code, only to show that I want to apply this sort of model to each group of records based on the SmpDate, Plot, Ch columns.
I have performed the calculations by splitting the ugN2O values for each SmpTime and binding them back to the dataframe as separate variables (columns) then using nested for loops and indexing to assign the SmpTime values and ugN2O values to y and x.  I was hoping for a more direct approach, if possible.
The result of LinMod should be 0.104731726 for SmpDate=="2019-06-04", Plot=="101",Ch=="1".

A: This fits a "many models" pattern, where we want to split our data into groups, apply a model to each group, and then extract the results of that model for a summary. 
In the tidyverse + tidymodels world, that's accomplished by combining:


*

*tidyr::nest(), which create columns of dataframes for each group

*purrr::map(), which allows us to apply a function to each element of a list/column.

*broom::tidy(), which constructs a more usable dataframe of model output

*tidyr::unnest(), which takes a dataframe and transforms into simple columns.


We have a couple of extra steps with your data specifically to handle areas where the values are all NA, and the linear model would normally throw errors back.
Code looks like:
# Wrap lm, since it throws errors
lmw <- function(data, formula) {
  tryCatch(lm(data = data, formula = formula), error = function(cond) NULL)
}  

library(tidyverse)
library(broom)
df %>% 
  nest(data = c(GHGEventID, SmpTime, TrtIDs, ugN2O, PK)) %>%
  mutate(lm_model = purrr::map(data, 
                               ~ broom::tidy(lmw(data = .x, ugN2O ~ SmpTime)))) %>%
  unnest(lm_model, keep_empty = TRUE) %>%
  filter(term != "(Intercept)" | is.na(estimate)) %>%
  mutate(LinMod = 60*24*10000/(0.144032*1000000)* estimate)

Output looks like:
# A tibble: 8 x 10
  SmpDate     Plot    Ch           data term    estimate std.error statistic p.value LinMod
  <fct>      <int> <int> <list<df[,5]>> <chr>      <dbl>     <dbl>     <dbl>   <dbl>  <dbl>
1 2019-06-04   101     1        [4 × 5] SmpTime  0.00105   0.00561     0.187  0.869   0.105
2 2019-06-04   101     2        [4 × 5] SmpTime -0.00164   0.00680    -0.241  0.832  -0.164
3 2019-06-04   202     1        [4 × 5] NA      NA        NA          NA     NA      NA    
4 2019-06-04   202     2        [4 × 5] NA      NA        NA          NA     NA      NA    
5 2019-06-19   101     1        [4 × 5] SmpTime  0.0189    0.00335     5.63   0.0302  1.89 
6 2019-06-19   101     2        [4 × 5] SmpTime  0.00726   0.00778     0.933  0.449   0.726
7 2019-06-19   202     1        [4 × 5] SmpTime  0.00691   0.00565     1.22   0.346   0.690
8 2019-06-19   202     2        [4 × 5] SmpTime  0.0238    0.00508     4.70   0.0424  2.38 

More on this pattern, especially using broom and the tidymodels approach here: summary dataframe from several multiple regression outputs. (And there are non tidymodels approaches too, as commenters have already added.)
