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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:02:34+00:00 2026-06-15T10:02:34+00:00

I have some events I am trying to enumerate in my data based on

  • 0

I have some events I am trying to enumerate in my data based on the order in which the occur, but conditioned on another variable (in this case Date) is not missing. I’ve somewhat stuck. My problem is that I need to do it only using base-R.

Here is an working example:

df <-
structure(list(Date = c("Sep 02 2012", "Sep 10 2012", "Sep 22 2012", 
"Sep 23 2012", "Sep 23 2012", "Sep 23 2012", "Sep 24 2012", "Sep 24 2012", 
"Sep 24 2012", "Dec 01 2012", "Sep 09 2014", NA), Event = c("005", 
"006", "002", "003", "004", "007", "008", "010", "011", "012", 
"009", "001"), Type = c("IND", "IND", "CON", "OUT", "OUT", "IND", 
"CMA", "ASH", "CON", "ASH", "CMA", "IND")), .Names = c("Date", 
"Event", "Type"), class = "data.frame", row.names = c(13L, 14L, 
10L, 11L, 12L, 15L, 16L, 18L, 19L, 20L, 17L, 9L))

Gives me this data frame,

df
          Date Event Type
13 Sep 02 2012   005  IND
14 Sep 10 2012   006  IND
10 Sep 22 2012   002  CON
11 Sep 23 2012   003  OUT
12 Sep 23 2012   004  OUT
15 Sep 23 2012   007  IND
16 Sep 24 2012   008  CMA
18 Sep 24 2012   010  ASH
19 Sep 24 2012   011  CON
20 Dec 01 2012   012  ASH
17 Sep 09 2014   009  CMA
9         <NA>   001  IND

What I would like to get,

new.df

       Date    Event Type Num_Type
13 Sep 02 2012   005  IND       1
14 Sep 10 2012   006  IND       2
10 Sep 22 2012   002  CON       1
11 Sep 23 2012   003  OUT       1
12 Sep 23 2012   004  OUT       2
15 Sep 23 2012   007  IND       3
16 Sep 24 2012   008  CMA       1
18 Sep 24 2012   010  ASH       1
19 Sep 24 2012   011  CON       1
20 Dec 01 2012   012  ASH       2
17 Sep 09 2014   009  CMA       2
9         <NA>   001  IND      NA
  • 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-15T10:02:35+00:00Added an answer on June 15, 2026 at 10:02 am

    You can do this with ave and cumsum:

    df$Num_Type <- 1
    df$Num_Type <- ave(df$Num_Type, df$Type, FUN=cumsum)
    is.na(df$Num_Type) <- which(is.na(df$Date))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data about a day's events that I'm trying to visualise as
I'm trying to get the histogram for some events I have indexed, but I
I'm trying to have a jQuery autocomplete. I have specified some data but when
I have some onchange events setup in jQuery which are used to populate a
I have a listener for click events from which I need to exclude some
I am trying to stop some events but stopPropagation does not work with live
I have some events like this class Granpa // this would not be changed,
I'm trying to load a PHP document dynamically which uses some jQuery events on
I have some Objective-C++ code that I'm trying to get events out of an
I have some elements with events bound. Before I remove those elements, do 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.