r - Creating Index based on Multiple Variables -
i looking creating indexes in r haven't found quite i'm looking index multiple variables. i'd reset when variable 1 changes well. have tried finaltablemba$index <- as.numeric(seq_along(finaltablemba$column_2)), can't seem figure out how add in second variable have index reset.
any appreciated!
for example, hoping accomplish following:
column 1 column 2 index 10/1/2013 10/1/2013 1 10/1/2013 10/2/2013 2..... 10/1/2013 10/30/2013 30 11/1/2013 11/1/2013 1 11/1/2013 11/2/2013 2
you can use by split data first column , apply tricky function each splitted group :
dat$index <- unlist(by(dat$column2,dat$column1,function(x) as.integer(as.factor(as.character(x))))) column1 column2 index 1 10/1/2013 10/1/2013 1 2 10/1/2013 10/2/2013 2 3 10/1/2013 10/30/2013 3 4 11/1/2013 11/1/2013 1 5 11/1/2013 11/2/2013 2 but think better format column2 regular date , use format(x,'%d') on it.
dat$index <- unlist(by(dat$column2,dat$column1,function(x) as.numeric(format(as.date(x,format='%m/%d/%y'),'%d'))))
Comments
Post a Comment