# How to get the first row of each group of a DataFrame and subtract that value from each subsequent row in that group?

**URL:** https://discourse.julialang.org/t/how-to-get-the-first-row-of-each-group-of-a-dataframe-and-subtract-that-value-from-each-subsequent-row-in-that-group/86527
**Category:** New to Julia
**Tags:** dataframes
**Created:** [August 30, 2022, 2:10am UTC](https://discourse.julialang.org/t/how-to-get-the-first-row-of-each-group-of-a-dataframe-and-subtract-that-value-from-each-subsequent-row-in-that-group/86527 "2022-08-30T02:10:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![akhibovska](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akhibovska/32/38256_2.png) [@akhibovska](https://discourse.julialang.org/u/akhibovska)
#### Post date: [August 30, 2022, 2:10am UTC](https://discourse.julialang.org/t/how-to-get-the-first-row-of-each-group-of-a-dataframe-and-subtract-that-value-from-each-subsequent-row-in-that-group/86527/1 "2022-08-30T02:10:04Z")

</div>

Hi!

Hope someone can help me figure out probably a pretty simple problem! I have a data set where I have a column called ‘Time’ and ‘Trial’. The time is recorded from the first trial in 4ms increments. I want to transform the ‘Time’ column so each trial begins with time 0. For that, I am trying to get the first-row value in column ‘Time’ for every trial in 144 trials and subtract that first value from each subsequent time value separately for each trial. I hope I make sense, I am more than happy to elaborate, but it should look something like this:

![Screen Shot 2022-08-29 at 10.08.12 PM](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a8ab046de2e0271faf8e5309d9865edc2bc53d8.png)

I tried: `groupby(loaddata, :Trial), :Time =>loaddata[:, :Time] .- loaddata[:1, :Time]` but it is probably very far from the right answer!

Thank you to anyone who can help me with this problem!!

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [August 30, 2022, 3:16am UTC](https://discourse.julialang.org/t/how-to-get-the-first-row-of-each-group-of-a-dataframe-and-subtract-that-value-from-each-subsequent-row-in-that-group/86527/2 "2022-08-30T03:16:41Z")

</div>

try

```julia
grouped = groupby(loaddata, :Trial)
newdf = transform(grouped, :Time => (t -> t .- first(t)), renamecols=false)

```

If you don’t want to have that extra variable hanging around you could do it using Chain.jl like

```julia
newdf = @chain loaddata begin
    groupby(:Trial)
    transform(:Time => first)
    select(:Time_first)
    .-(loaddata.:Time, _)
end

```

Note that if your data is not sorted on `:Time` you may want to use `minimum` instead of `first`

---

<div class="post-metadata">

### Author: ![akhibovska](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akhibovska/32/38256_2.png) [@akhibovska](https://discourse.julialang.org/u/akhibovska)
#### Post date: [August 30, 2022, 4:36am UTC](https://discourse.julialang.org/t/how-to-get-the-first-row-of-each-group-of-a-dataframe-and-subtract-that-value-from-each-subsequent-row-in-that-group/86527/3 "2022-08-30T04:36:05Z")

</div>

Thank you so much for your reply! I’ll try this! I’m very new to Julia and was looking for this solution for a while, I’ll look up all functions too! I assume I can also make a new variable in my data frame that will nest the transformed time?

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [August 30, 2022, 4:44am UTC](https://discourse.julialang.org/t/how-to-get-the-first-row-of-each-group-of-a-dataframe-and-subtract-that-value-from-each-subsequent-row-in-that-group/86527/4 "2022-08-30T04:44:08Z")

</div>

if you want the adjusted time to be a new variable, then you can just omit the `renamecols=false` part, as by default that line will create a new column called `:Time_first`. you can also use the `Pair` syntax to customize the name, like so

`transform(grouped, :Time => (t -> t .- first(t)) => :AdjTime)`. Just be very sure you put those parentheses around the lambda! in Julia, `=>` has higher precedence than `->`.

For the second way, that actually just returns the new column itself, so you could modify `loaddata` in place like

```julia
loaddata[!, :AdjTime] = @chain loaddata begin
    groupby(:Trial)
    transform(:Time => first)
    select(:Time_first)
    .-(loaddata.:Time, _)
end

```
