2

I am a newbie to R. I am trying to find a way on how to process each row within a group using dplyr package.

I have grouped the data as below. Now, I would want to create a new column which will have a incremental value for each row depending on the value in col_date

grpd_data <- master_data %>% group_by(col_str,col_date)

As an example, consider the below data set:

col1  col2 col_str   col_date
a1     a2   grp1    05-11-2015
b1     b2   grp1    06-11-2015
c1     c2   grp2    05-11-2015
d1     d2   grp2    06-11-2015

I am looking for an output like the below:

col1  col2 col_str   col_date   grp_seq
a1     a2   grp1    05-11-2015   1
b1     b2   grp1    06-11-2015   2
c1     c2   grp2    05-11-2015   1
d1     d2   grp2    06-11-2015   2

Further, grp_seq should get values incremented based on the value in col_date. If the value is of date_time, then first record within the day would get a value of 1, next record would get 2 and so on

Please let me know whats the best way to achieve the same.

lmo
  • 37,904
  • 9
  • 56
  • 69

1 Answers1

3

You don't want to include col_date in your groups, and then you can add on a row number. Try this:

library(dplyr)
master_data %>% group_by(col_str) %>%
                mutate(grp_seq = row_number())
jeremycg
  • 24,657
  • 5
  • 63
  • 74