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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:22:32+00:00 2026-06-18T08:22:32+00:00

So this question has been bugging me for a while since I’ve been looking

  • 0

So this question has been bugging me for a while since I’ve been looking for an efficient way of doing it. Basically, I have a dataframe, with a data sample from an experiment in each row. I guess this should be looked at more as a log file from an experiment than the final version of the data for analyses.

The problem that I have is that, from time to time, certain events get logged in a column of the data. To make the analyses tractable, what I’d like to do is “fill in the gaps” for the empty cells between events so that each row in the data can be tied to the most recent event that has occurred. This is a bit difficult to explain but here’s an example:

Screenshot of dataframe from RStudio of base dataset

Now, I’d like to take that and turn it into this:

enter image description here

Doing so will enable me to split the data up by the current event. In any other language I would jump into using a for loop to do this, but I know that R isn’t great with loops of that type, and, in this case, I have hundreds of thousands of rows of data to sort through, so am wondering if anyone can offer suggestions for a speedy way of doing this?

Many thanks.

  • 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-18T08:22:33+00:00Added an answer on June 18, 2026 at 8:22 am

    The na.locf() function in package zoo is useful here, e.g.

    require(zoo)
    dat <- data.frame(ID = 1:5, sample_value = c(34,56,78,98,234),
                      log_message = c("FIRST_EVENT", NA, "SECOND_EVENT", NA, NA))
    
    dat <-
      transform(dat,
                Current_Event = sapply(strsplit(as.character(na.locf(log_message)), 
                                                "_"),
                                       `[`, 1))
    

    Gives

    > dat
      ID sample_value  log_message Current_Event
    1  1           34  FIRST_EVENT         FIRST
    2  2           56         <NA>         FIRST
    3  3           78 SECOND_EVENT        SECOND
    4  4           98         <NA>        SECOND
    5  5          234         <NA>        SECOND
    

    To explain the code,

    1. na.locf(log_message) returns a factor (that was how the data were created in dat) with the NAs replaced by the previous non-NA value (the last one carried forward part).
    2. The result of 1. is then converted to a character string
    3. strplit() is run on this character vector, breaking it apart on the underscore. strsplit() returns a list with as many elements as there were elements in the character vector. In this case each component is a vector of length two. We want the first elements of these vectors,
    4. So I use sapply() to run the subsetting function '['() and extract the 1st element from each list component.
    5. The whole thing is wrapped in transform() so i) I don;t need to refer to dat$ and so I can add the result as a new variable directly into the data dat.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been bugging me for a while Is it usual to have
This question has been bugging me for a while. I am looking for a
This is a question that has been bugging me for a while. I started
This is a question that has been bugging me for a while and I
I have a question that has been bugging me for a while now. I
This question has been bugging me for a while now. When writing a CSS
This question has been bugging me for a while: I've read in MSDN's DirectX
This question has been bothering me for quite a while: I have a Room
This question has been bugging me for a while now, and I haven't found
This question has been bugging me for a long time now but essentially I'm

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.