Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:01:27+00:00 2026-06-14T19:01:27+00:00

I have data arranged like this in R: indv time val A 6 5

  • 0

I have data arranged like this in R:

indv    time    val
A          6    5
A         10    10
A         12    7
B          8    4
B         10    3
B         15    9

For each individual (indv) at each time, I want to calculate the change in value (val) from the initial time. So I would end up with something like this:

indv time   val val_1   val_change
A       6     5    5       0
A      10    10    5       5
A      12     7    5       2
B       8     4    4       0
B      10     3    4      -1
B      15     9    4       5

Can anyone tell me how I might do this? I can use

ddply(df, .(indv), function(x)x[which.min(x$time), ])

to get a table like

indv    time    val
A          6    5   
B          8    4   

However, I cannot figure out how to make a column val_1 where the minimum values are matched up for each individual. However, if I can do that, I should be able to add column val_change using something like:

df['val_change'] = df['val_1'] - df['val']

EDIT: two excellent methods were posted below, however both rely on my time column being sorted so that small time values are on top of high time values. I’m not sure this will always be the case with my data. (I know I can sort first in Excel, but I’m trying to avoid that.) How could I deal with a case when the table appears like this:

indv    time    value
A          10   10
A           6   5
A          12   7
B           8   4
B          10   3
B          15   9
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:01:28+00:00Added an answer on June 14, 2026 at 7:01 pm

    Here’s a plyr solution using ddply

    ddply(df, .(indv), transform, 
          val_1 = val[1],
          change = (val - val[1]))
    
      indv time val val_1 change
    1    A    6   5     5      0
    2    A   10  10     5      5
    3    A   12   7     5      2
    4    B    8   4     4      0
    5    B   10   3     4     -1
    6    B   15   9     4      5
    

    To get your second table try this:

    ddply(df, .(indv), function(x) x[which.min(x$time), ])
      indv time val
    1    A    6   5
    2    B    8   4
    

    Edit 1

    To deal with unsorted data, like the one you posted in your edit try the following

    unsort <- read.table(text="indv    time    value
    A          10   10
    A           6   5
    A          12   7
    B           8   4
    B          10   3
    B          15   9", header=T)
    
    
    do.call(rbind, lapply(split(unsort, unsort$indv), 
                      function(x) x[order(x$time), ]))
        indv time value
    A.2    A    6     5
    A.1    A   10    10
    A.3    A   12     7
    B.4    B    8     4
    B.5    B   10     3
    B.6    B   15     9
    

    Now you can apply the procedure described above to this sorted dataframe

    Edit 2

    A shorter way to sort your dataframe is using sortBy function from doBy package

    library(doBy)
    orderBy(~ indv + time, unsort)
      indv time value
    2    A    6     5
    1    A   10    10
    3    A   12     7
    4    B    8     4
    5    B   10     3
    6    B   15     9
    

    Edit 3

    You can even sort your df using ddply

    ddply(unsort, .(indv, time), sort)
      value time indv
    1     5    6    A
    2    10   10    A
    3     7   12    A
    4     4    8    B
    5     3   10    B
    6     9   15    B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data arranged like this in R: indv time mass 1 10 7
I have a R data frame that looks like this: Country Property Value Canada
i have data-grid in my application and i want to customize my data-grid so
I have 'data.xlsx' file with cells of numbers and letters. when I run this
I have data from travel diaries which has been read in from a csv
I have data coming from an external system (in CSV form). The data contains
I have data like the following: var data = [{ id: 1, date: new
I have data from two different sources that I need to combine. Some data
I have data with no relation. Just need to count various columns from 3
I have an inline assembler loop that cumulatively adds elements from an int32 data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.