虚拟系列中的第一个新元素 [英] Dummy for first new element in a series

查看:79
本文介绍了虚拟系列中的第一个新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个持续数个周期的变量. 就像我拥有Ipod的年限一样. 所以从2001年到2004年,我有了Ipod 1代,然后在2005年,我有了Ipod 2,依此类推.所以我的数据框看起来像:

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?

谢谢

推荐答案

这是怎么回事:

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])

这篇关于虚拟系列中的第一个新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆