Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Generate column id

$
0
0

I am working with log data; trying to find the round number of each event. The start of a round is signaled by action=="start". I want to create a "action.round" columns that tells me which round each event corresponds to.

I have data such this:

    data <- read_table2("Id action 
A   start
A   na
A   start
A   na
A   na
A   na
A   na
A   start
B   start
B   na
B   start
B   na
B   start
B   na"

I am trying to create an output such as this:

output <- read_table2("Id   action  action.round
A   start   1
A   na  1
A   start   2
A   na  2
A   na  2
A   na  2
A   na  2
A   start   3
B   start   1
B   na  1
B   start   2
B   na  2
B   start   3
B   na  3")

So far, I have been able to get part of the output by using row_number(), like this:

` data %>% 
  mutate(round.start=case_when(actionValue=="start"~"start",TRUE~"NA")) %>%
  ungroup() %>%
  group_by(Id,round.start) %>%
  mutate(action.round=row_number())`

But now, I would like to fill the round number that corresponds to round.start=="start" into the column, so that I know which round number each column actually corresponds to (see desired output above).


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>