4
$\begingroup$

Given data={{1, 34}, {2, 54}, {3, 66}, {4, 77}, {5, 92}}, I would like to create a new list such that

{{1, 34}, {2, 54-34}, {3, 66-54}, {4, 77-66}, {5, 92-77}} 

which is {{1, 34}, {2, 20}, {3, 12}, {4, 11}, {5, 15}.

$\endgroup$
4
$\begingroup$
ClearAll[f]
f = SubsetMap[Differences @* Prepend[0], {All,2}]

Example:

data = {{1, 34}, {2, 54}, {3, 66}, {4, 77}, {5, 92}} ;

f @ data
  {{1, 34}, {2, 20}, {3, 12}, {4, 11}, {5, 15}} 
| improve this answer | |
$\endgroup$
5
$\begingroup$

The following works, I think

data = {{1, 34}, {2, 54}, {3, 66}, {4, 77}, {5, 92}};
data[[2 ;; 5, 2]] = Differences[data[[All, 2]]];

Output when data is called is

{{1, 34}, {2, 20}, {3, 12}, {4, 11}, {5, 15}}

Adjusting for specific lengths (dimensions of a submatrix to be replaced) is straightforward, e.g.

data[[2 ;; Dimensions[data][[1]], 2]] = Differences[data[[All, 2]]];
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.