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

try

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

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

1 Like