Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Revert time column to 0 every time another column's text changes R

In R, I have a data frame with a time column that I want to revert to one or zero every time the text in another column changes. The text never goes back to the previous text.
There are 395 times the text changes and ~270’000 rows.

Table at the moment

Time Trial
11 A
12 A
13 B
14 B

Table wanted

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Time Trial
1 A
2 A
1 B
2 B

>Solution :

We could group and then use row_number()

library(dplyr)

df %>% 
  group_by(Trial) %>% 
  mutate(Time = row_number()) %>%
  ungroup()
   Time Trial
  <int> <chr>
1     1 A    
2     2 A    
3     1 B    
4     2 B  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading