0
我试图根据某些条件创建一个新变量。以下是数据的一个示例。根据一定的条件创建新变量
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。如果这一条件得到满足我想要的变量“当”被“dot.com”,为2000年及以后的所有这个特定国家的年份。如果上面的条件不满足,我想'时'变量为“其他”。
这是我想达到的。
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")