creating new column in an R dataframe based on values in multiple columns -
i have following data frame including restaurants (id#), owner , purchase date. restaurant changes ownership, analysis purposes want create column keeps name of latest owner, determined "purchase date". how can create new column? in example restaurant 23 changes hand on 1/1/2013, want include new owner's name (bob) current owner rows restaurant shown below. if restaurant not change hands, keep same name "current owner" column "owner" column.
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")
you can use this:
do.call(rbind, by(df, df$restaurant, function(d) within(d, currentowner2 <- as.character( owner[which.max(as.date(purchasedate, format="%d/%m/%y"))]))))
thanks @thomas excavating question :-)
Comments
Post a Comment