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

Find difference between consecutive rows after grouping columns that starts_with multiple argument

I want to group my df based on the column clone, and also based on what the type starts with, anc and ev.

After I group based on these conditions, I want to find the difference between consecutive rows

library(tidyverse)

set.seed(123)
df <- tibble(
  anc_0=c(runif(6,0.045,0.048)), 
  anc_3=c(runif(6,0.045,1.56)), 
  ev_0=c(runif(6,0.045,0.048)), 
  ev_3=c(runif(6,0.045,1.56)), 
  clone=c(rep(c("clone1","clone2"), each=3))
)

df %>% 
  pivot_longer(!clone, names_to = "type", values_to = "OD") %>% 
  group_by(across(starts_with("anc")), across(starts_with("ev")), clone) %>% 
  mutate(diff= OD - lag(OD))
#> # A tibble: 24 × 4
#> # Groups:   clone [2]
#>    clone  type      OD   diff
#>    <chr>  <chr>  <dbl>  <dbl>
#>  1 clone1 anc_0 0.0459 NA    
#>  2 clone1 anc_3 0.845   0.799
#>  3 clone1 ev_0  0.0470 -0.798
#>  4 clone1 ev_3  0.542   0.495
#>  5 clone1 anc_0 0.0474 -0.494
#>  6 clone1 anc_3 1.40    1.35 
#>  7 clone1 ev_0  0.0467 -1.35 
#>  8 clone1 ev_3  1.49    1.44 
#>  9 clone1 anc_0 0.0462 -1.44 
#> 10 clone1 anc_3 0.880   0.834
#> # … with 14 more rows

Created on 2022-12-25 with reprex v2.0.2

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

I want my data to look like this

#>    clone  type      OD   diff
#>    <chr>  <chr>  <dbl>  <dbl>
#>  1 clone1 anc_0 0.0459 NA    
#>  2 clone1 anc_3 0.845   0.799
#>  3 clone1 ev_0  0.0470 NA
#>  4 clone1 ev_3  0.542   0.495
#>  5 clone1 anc_0 0.0474 NA
#>  6 clone1 anc_3 1.40    1.35 
#>  7 clone1 ev_0  0.0467 NA
#>  8 clone1 ev_3  1.49    1.44 
#>  9 clone1 anc_0 0.0462 NA 
#> 10 clone1 anc_3 0.880   0.834
#> # … with 14 more rows

any idea or help is appreciated

>Solution :

Try

library(dplyr)
library(tidyr)
library(data.table)
 df %>% 
  pivot_longer(!clone, names_to = "type", values_to = "OD") %>% 
  group_by(clone, grp2 = rleid(str_remove(type, '_\\d+'))) %>% 
  mutate(diff = OD - lag(OD)) %>%
  ungroup %>%
  select(-grp2)

-output

# A tibble: 24 × 4
   clone  type      OD   diff
   <chr>  <chr>  <dbl>  <dbl>
 1 clone1 anc_0 0.0459 NA    
 2 clone1 anc_3 0.845   0.799
 3 clone1 ev_0  0.0470 NA    
 4 clone1 ev_3  0.542   0.495
 5 clone1 anc_0 0.0474 NA    
 6 clone1 anc_3 1.40    1.35 
 7 clone1 ev_0  0.0467 NA    
 8 clone1 ev_3  1.49    1.44 
 9 clone1 anc_0 0.0462 NA    
10 clone1 anc_3 0.880   0.834
# … with 14 more rows
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