我有以下数据框,包括餐厅 (id#)、所有者和购买日期。有时餐厅会更改所有权,但出于分析目的,我想创建另一个仅保留最新所有者姓名的列,这由“购买日期”确定。如何创建这个新列?在此示例中,餐厅 23 于 2013 年 1 月 1 日易手,因此我只想包含新所有者的姓名 (Bob) 作为该餐厅所有行的当前所有者,如下所示。如果餐厅没有易手,请保持“当前所有者”列与“所有者”列的名称相同。
Restaurant <- c(23,23,23,23,23,23,23,15,15,15,15,17,17,17,17)
owner <- c("Chuck","Chuck","Chuck","Chuck","Bob","Bob","Bob",
"Hazel","Hazel","Hazel","Hazel","Pete","Pete","Pete","Pete")
purchaseDate <- c("3/4/2011","3/4/2011","3/4/2011","3/4/2011",
"1/1/2013","1/1/2013","1/1/2013","4/11/2010","4/11/2010","4/11/2010",
"4/11/2010","9/2/2012","9/2/2012","9/2/2012","9/2/2012")
df <- data.frame( Restaurant = Restaurant, owner=owner, purchaseDate=purchaseDate)
df$currentOwner <- c("Bob","Bob","Bob","Bob","Bob","Bob","Bob","Hazel","Hazel","Hazel","Hazel","Pete","Pete","Pete","Pete")