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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:26:24+00:00 2026-06-04T19:26:24+00:00

Good day, d <- c(1,1,1,2,2,2,3,3,3) e <- c(5,6,7,5,6,7,5,6,7) f <- c(0,0,1,0,1,0,0,0,1) df <- data.frame(d,e,f)

  • 0

Good day,

d <- c(1,1,1,2,2,2,3,3,3)
e <- c(5,6,7,5,6,7,5,6,7)
f <- c(0,0,1,0,1,0,0,0,1)
df <- data.frame(d,e,f)

I have data the looks like the above. What I need to do is for each unique element of d find the first non-zero value in f, and find the corresponding value in e. To be specific, I want another vector g so it looks like this:

d <- c(1,1,1,2,2,2,3,3,3)
e <- c(5,6,7,5,6,7,5,6,7)
f <- c(0,0,1,0,1,0,0,0,1)
g <- c(7,7,7,6,6,6,7,7,7)
df <- data.frame(d,e,f,g)

Suggestions to do this easily? I thought I could use split(), but I am having trouble using which() after the split. I can use ave like this:

foo <- function(x){which(x>0)[1]}
df$t <- ave(df$f,df$d,FUN=foo)

But I am having trouble finding the value of e. Any help is appreciated.

  • 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-04T19:26:28+00:00Added an answer on June 4, 2026 at 7:26 pm

    Someone else can provide a base R solution, but here’s a way to do this using plyr:

    > ddply(df,.(d),transform,g = head(e[f != 0],1))
      d e f g
    1 1 5 0 7
    2 1 6 0 7
    3 1 7 1 7
    4 2 5 0 6
    5 2 6 1 6
    6 2 7 0 6
    7 3 5 0 7
    8 3 6 0 7
    9 3 7 1 7
    

    Note that I took your note about the “first nonzero element” literally, even though your example data only had a single unique nonzero element in the column (by group).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, I've got WebMethod that looks a bit like this... [WebMethod] public static
Good day everyone, I have the following XML data : <?xml version=1.0 encoding=utf-8 ?>
good day sirs. i have a small program that reads and processes data via
Good day, I have been given an assignment that gets data off a CSV
Good day! I am having a problem in showing the data that I need
Good day, i have mongodb database filled with some data, i ensured that data
Good day. I have an NSMutableArray that contains references to objects. I need to
Good Day, I have a sql query that returns the following data: EmployeeID...Employee 555..........John
Good day! I would like to have your suggestions with regards to this matter.
Good day, I receive data from a communication channel and display it. Parallel, 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.