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

  • SEARCH
  • Home
  • 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 9194157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:20:25+00:00 2026-06-17T21:20:25+00:00

I have the data frame containing longitudinal measurements of variables x and y ,

  • 0

I have the data frame containing longitudinal measurements of variables x and y, at various time points time, in several subjects id. However x and y have some missing values.

What I want is to aggregate the data frame so that for each id i get the first in time defined x and y value. x and y would be then at different time points but it does not matter.

testdf<-data.frame(id=c(rep("A",4),rep("B",4),rep("C",4) ), x=c(NA, NA, 1,2, 3, NA, NA, 1, 2, NA,NA, 5), y=rev(c(NA, NA, 1,2, 3, NA, NA, 1, 2, NA,NA, 5)), time=c(1,2,3,4,0.1,0.5,10,20,3,2,1,0.5))

So that testdf would reduce to

 id x y
1  A 1 5
2  B 3 1
3  C 5 1

UPDATE: Would it be possible for a solution that allows the data frame to have a large number of variables (a solution or a function where you don’t have to explicitly defining thex and y variables in case the data frame has a large number of variables?

  • 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-17T21:20:26+00:00Added an answer on June 17, 2026 at 9:20 pm

    Is this what you want?

    > library(plyr)
    > ddply(testdf, .(id), summarize, x = na.omit(x)[1], y = na.omit(y)[1])
      id x y
    1  A 1 5
    2  B 3 1
    3  C 2 2
    

    UPDATED

    Here is the implicit version.

    > ddply(subset(testdf, select = id:y), .(id), colwise(function(z) na.omit(z)[1]))
      id x y
    1  A 1 5
    2  B 3 1
    3  C 2 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame containing multiple time series of returns, stored in columns.
I have a data frame with several factor columns containing NaN 's that I
I have a data frame called VarChange containing 1005 variables such as: row.name SamplingEvent
I have a Rdata file containing various objects: New.Rdata |_ Object 1 (e.g. data.frame)
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data.frame of a time series of data, I would like to
I have a data frame containing (in random places) a character value (say foo
I have a data frame containing 10k rows, for a given column X I

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.