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