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 6835357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:13:22+00:00 2026-05-26T23:13:22+00:00

How do I select the first row of an R data frame that meets

  • 0

How do I select the first row of an R data frame that meets certain criteria?

Here is the context:

I have a data frame with five columns:

"pixel", "year","propvar", "component", "cumsum." 

There are 1,225 combinations of pixel and year, because the data was computed from the annual time series of 49 geographic pixels for each of 25 study years. Within each pixel-year, I have computed propvar, the proportion of total variance explained by a given component of the fast Fourier transform for the time series of a given pixel-year. I then computed cumsum, which is the cumulative sum of propvar for each frequency component within a pixel-year. The component column just gives you an index for the Fourier series component (plus 1) from which propvar was calculated.

I want to determine the number of components required to explain greater than 99% of the variance. I figure one way to do this is to find the first row within each pixel-year where cumsum > 0.99, and create a data frame from it with three columns, pixel, year, and numbercomps, where numbercomps is the number of components required within a given pixel-year to explain greater than 99% of the variance. I do not know how to do this in R. Does anyone have a solution?

  • 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-05-26T23:13:23+00:00Added an answer on May 26, 2026 at 11:13 pm

    Sure. Something like this should do the trick:

    # CREATE A REPRODUCIBLE EXAMPLE!
    df <- data.frame(year = c("2001", "2003", "2001", "2003", "2003"),
                     pixel = c("a", "b", "a", "b", "a"), 
                     cumsum = c(99, 99, 98, 99, 99),
                     numbercomps=1:5)
    df
    #   year pixel cumsum numbercomps
    # 1 2001     a     99           1
    # 2 2003     b     99           2 
    # 3 2001     a     98           3
    # 4 2003     b     99           4
    # 5 2003     a     99           5
    
    # EXTRACT THE SUBSET YOU'D LIKE.
    res <- subset(df, cumsum>=99)
    res <- subset(res, 
                  subset = !duplicated(res[c("year", "pixel")]),
                  select = c("pixel", "year", "numbercomps"))
    #   pixel year numbercomps
    # 1     a 2001           1
    # 2     b 2003           2
    # 5     a 2003           5
    

    EDIT Also, for those interested in data.table, there is this:

    library(data.table)
    dt <- data.table(df, key="pixel, year")    
    dt[cumsum>=99, .SD[1], by=key(dt)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I select the first 4 rows of a data.frame : Weight Response
SQLDF newbie here. I have a data frame which has about 15,000 rows and
the row source for a listbox looks like this: SELECT users.id, users.first, users.last, chavrusas.luser_type
I have first: select url from urls where site = '$value' order by url
I have 2 queries. First: SELECT * FROM `table` WHERE col='xyz' LIMIT 100 //Time
How can I select the first and last row for each unique id in
I want to select the entire first row of each record where a promo
I have a nightly SSIS process that exports a TON of data from an
I have two SAS data tables. The first has many millions of records, and
Here is the example data set i have : id | messageread | timestamp

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.