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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:07:34+00:00 2026-05-29T07:07:34+00:00

Suppose I have a variable that lasts for several periods. Like the amount of

  • 0

Suppose I have a variable that lasts for several periods.
Like the amount of years that I have an Ipod.
So I had the Ipod 1st generation from 2001 until 2004 and then in 2005 I’ve got Ipod 2 and so on. So my dataframe would look like:

  2001 Ipod1
  2002 Ipod1
  2003 Ipod1
  2004 Ipod1
  2005 Ipod2
  2006 Ipod2
  2007 Ipod2
  2008 Ipod2
  2009 Ipod3
  2010 Ipod3

What I want is to create a dummy for the period when a new variable arrives so I would get:

  Year  Var  Dummy
  2001 Ipod1  1
  2002 Ipod1  0
  2003 Ipod1  0
  2004 Ipod1  0
  2005 Ipod2  1
  2006 Ipod2  0
  2007 Ipod2  0
  2008 Ipod2  0
  2009 Ipod3  1
  2010 Ipod3  0

So far I have been able to do this:

df = structure(list(Year = 2001:2010, Var = structure(c(1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("Ipod1", "Ipod2", "Ipod3"
), class = "factor")), .Names = c("Year", "Var"), class = "data.frame", row.names = c(NA,
-10L))

df$number.in.group = unlist(lapply(table(df$Var),seq.int)) 
df$dummy = ifelse(df$number.in.group == 1,1,0)
df$dummy[1]=0

Actually I would like the first element of the dummy to be zero.

My question is: Is there any way of doing this in a better way?

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-05-29T07:07:35+00:00Added an answer on May 29, 2026 at 7:07 am

    How about this:

    df$Dummy <- as.numeric(!duplicated(df$Var))
    
    # Or, if you want the first element to be 0,
    df$Dummy <- c(0, as.numeric(!duplicated(df$Var))[-1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have an integer variable in a class, and this variable may
Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose I have a variable that's already been initialized to a string via an
Suppose you have a trigger on MY_CUSTOMER_TABLE and that it has a variable declared
Suppose I have a variable counter, and there are several threads accessing and setting
Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
Suppose I have a variable of type Int = 08, how can I convert
Suppose i have a LPSTR variable. How do i free the memory after using
Suppose I have a static variable declared inside a function in C. If I
In Java, suppose I have a String variable S, and I want to search

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.