How to apply differences on part of a list and keep the rest?
Perhaps this?:
l1 = {{a, b, 3, c}, {e, f, 5, k}, {n, k, 12, m}, {s, t, 1, y}};
l2 = Differences[l1[[All, 3 ;;]]] /. b_ - a_ :> Sequence[a, b]
(* {{2, c, k}, {7, k, m}, {-11, m, y}} *)
It assumes the letter symbols are simple and not complicated expressions.
This is more complicated, but more robust:
Flatten /@
Transpose@
MapAt[Differences,
Partition[Transpose@l1[[All, 3 ;;]], {1, 2}, {1, 1}], {1, All, 1}]
You can also use BlockMap
as follows:
BlockMap[{#[[3]].{-1, 1}, ## & @@ Flatten@#[[4 ;;]]} &@* Transpose, l1, 2, 1]
{{2, c, k}, {7, k, m}, {-11, m, y}}
or
BlockMap[{#[[1]].{-1, 1}, ## & @@ Flatten@ #[[2 ;;]]} &@*Transpose, l1[[All, 3 ;;]], 2, 1]
{{2, c, k}, {7, k, m}, {-11, m, y}}
This is very similar to kglr's first solution but picks the relevant quantities a bit more explicitly:
l2 = BlockMap[{#[[2, 3]] - #[[1, 3]], #[[1, 4]], #[[2, 4]]} &, l1, 2, 1]
{{2, c, k}, {7, k, m}, {-11, m, y}}
With a parameter to change the symbolic column quickly:
l2 = With[{col = 3},
BlockMap[{#[[2,col]] - #[[1,col]], #[[1,col+1]], #[[2,col+1]]} &, l1, 2, 1]]
{{2, c, k}, {7, k, m}, {-11, m, y}}