dataframe - Datewise grouping data in R -
i have sample dataframe:
date item1 item2 item3 17-11-2016 2a hp cnf 12-11-2016 1a bp wl 13-11-2016 3a sp dl 14-11-2016 1a hp cnf 16-11-2016 2a bp cnf 10-11-2016 1a sp wl 17-11-2016 2a hp wl
i want group data based on columns date, item1 , item2, particular column same value come same group.
expected output:
date item1 item2 item3 grp 17-11-2016 2a hp cnf 1 17-11-2016 2a hp wl 1 12-11-2016 1a bp wl 2 13-11-2016 3a sp dl 3 14-11-2016 1a hp cnf 4 16-11-2016 2a bp cnf 5 10-11-2016 1a sp wl 6
you can way:
df <- data.frame(date = c("17-11-2016","12-11-2016","13-11-2016","14-11-2016", "16-11-2016","10-11-2016","17-11-2016"), item1 = c("2a","1a","3a","1a","2a","1a","2a"), item2 = c("hp","bp","sp","hp","bp","sp","hp"), item3 = c("cnf","wl","dl","cnf","cnf","wl","wl") ) df$grp <- as.numeric(factor(paste(df$date,df$item1,df$item2))) df[order(df$grp),] date item1 item2 item3 grp 6 10-11-2016 1a sp wl 1 2 12-11-2016 1a bp wl 2 3 13-11-2016 3a sp dl 3 4 14-11-2016 1a hp cnf 4 5 16-11-2016 2a bp cnf 5 7 17-11-2016 2a hp wl 6 1 17-11-2016 2a hp cnf 6
Comments
Post a Comment