我有以下数据框,包括餐馆(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")
答案 0 :(得分:0)
您可以使用:
do.call(rbind, by(df, df$Restaurant, function(d)
within(d, currentOwner2 <- as.character(
owner[which.max(as.Date(purchaseDate, format="%d/%m/%Y"))]))))
感谢@Thomas挖掘这个问题: - )