2013-05-08 75 views
0

我有以下數據框,其中包括餐館(id#),所有者和購買日期。有時餐廳會更改所有權,但爲了分析目的,我想創建另一個只保留最新所有者名稱的列,這由「購買日期」決定。我如何創建這個新列?在這個例子中,餐館23在2013年1月1日改變了手,所以我想只包括新所有者的名字(Bob)作爲該餐廳的所有行的當前所有者,如下所示。如果餐廳不換手,請爲​​「當前所有者」列保留與「所有者」列相同的名稱。基於多列中的值在R數據框中創建新列

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

您可以使用此:

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爲挖掘這個問題:-)

+0

完美!謝謝。 – 2013-08-14 17:12:46

相關問題