根据特定条件创建新变量

时间:2016-09-06 10:56:31

标签: r

我试图根据某些条件创建一个新变量。以下是数据样本。

df <- data.frame(country = rep(c("A","B"), each = 9),
       year = c(2000,2001,2008,2009,2010,2011,2012,2013,2014,1995,1996,1997,1998,1999,2000,2001,2002,2003),
       number = c(1,2,9,10,11,12,13,14,15,1,2,3,4,5,6,7,8,9))

我想要做的是检查年份= 2000和数字= 1.如果满足这个条件我想要变量&#39;当&#39;成为&#34; dot.com&#34; 2000年以及该特定国家的所有后续年份。如果不满足上述条件,我想要&#39;何时&#39;变量为&#34;其他&#34;。

这是我想要实现的目标。

structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), year = c(2000, 2001, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 
2002, 2003), number = c(1, 2, 9, 10, 11, 12, 13, 14, 15, 1, 2, 
3, 4, 5, 6, 7, 8, 9), when = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("dot.com", 
"other"), class = "factor")), .Names = c("country", "year", "number", 
"when"), row.names = c(NA, -18L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

我们可以尝试$ npm --version && node --version 3.9.5 v6.2.2

data.table