Q:

na.trim on specific column with data.table

I'm trying to use na.trim from package zoo on a single column on a data.table object. Here's what my data looks like:
DT <- data.table(id=c(rep("a",3),rep("b",3)),
                 col1=c(NA,1,2,NA,3,NA),col2=c(NA,NA,5,NA,NA,NA))
   id col1 col2
1:  a   NA   NA
2:  a    1   NA
3:  a    2    5
4:  b   NA   NA
5:  b    3   NA
6:  b   NA   NA

I would like to remove rows with leading NAs using na.trim and group by id. Here's the result I'm expecting:
   id col1 col2
1:  a    1   NA
2:  a    2    5
3:  b    3   NA
4:  b   NA   NA

Here's what I have tried so far. This removes leading NAs but removes col2:
DT[,na.trim(col1),by=id]
   id V1
1:  a  1
2:  a  2
3:  b  3

This is also not working:
DT[,.SD[na.trim(col1)],by=id]
   id col1 col2
1:  a   NA   NA
2:  a    1   NA
3:  b   NA   NA

A:

A possible solution without using the zoo-package:
DT[DT[, .I[!!cumsum(!is.na(col1))], by = id]$V1]

you get:
   id col1 col2
1:  a    1   NA
2:  a    2    5
3:  b    3   NA
4:  b   NA   NA

What this does:

With DT[, .I[!!cumsum(!is.na(col1))], id]$V1 you create a vector of rownumbers to keep. By using !!cumsum(!is.na(col1)) you make sure that only the leading missing values of col1 are omitted.
Next you use that vector to subset the data.table.
!!cumsum(!is.na(col1)) does the same as cumsum(!is.na(col1))!=0. Using !! converts all number higher than zero to TRUE and all zeros to FALSE.
.I isn't necessarily needed, you can also use: DT[DT[, !!cumsum(!is.na(col1)), by = id]$V1] which subsets the data.table with a logical vector.

Two alternatives with cummax by @lmo from the comments:
# alternative 1:
DT[DT[, !!(cummax(!is.na(col1))), by = id]$V1]

# alternative 2:
DT[as.logical(DT[, cummax(!is.na(col1)), by = id]$V1)]

Another alternative by @jogo:
DT[, .SD[!!cumsum(!is.na(col1))], by = id]

Another alternative by @Frank:
DT[, .SD[ rleid(col1) > 1L | !is.na(col1) ], by = id]

A:

na.trim would be used like this with data.table.  See ?na.trim for more info on its arguments.
DT[, na.trim(.SD, sides = "left", is.na = "all"), by = id]

giving:
   id col1 col2
1:  a    1   NA
2:  a    2    5
3:  b    3   NA
4:  b   NA   NA

ADDED:
In comment poster clarified that only column 1 NAs should be operated on by na.trim.  In that case append a column of row numbers, .I, and after involing na.trim subset using those row numbers.
DT[DT[, na.trim(data.table(col1, .I), "left"), by = id]$.I, ]

