How to make r-script to make outcome

Hello everyone! I am beginner of R
Could someone help me to make script.
I am trying to make one more column which is 'outcome'
It mark 'Y' among minimum seq number in claim no.

image

The package dplyr has handy functions for this sort to of thing.

library(dplyr, warn.conflicts = FALSE)
Df <- data.frame(Claim = c("A", "A", "B", "B", "C", "C", "C", "D", "D", "E", "E"),
                 seq = c(1,2,1,2,2,3,4,4,5,1,2))
Df <- Df %>% group_by(Claim) %>%  
  mutate(outcome = ifelse(seq == min(seq), "Y", ""))
print(Df)
#> # A tibble: 11 x 3
#> # Groups:   Claim [5]
#>    Claim   seq outcome
#>    <chr> <dbl> <chr>  
#>  1 A         1 "Y"    
#>  2 A         2 ""     
#>  3 B         1 "Y"    
#>  4 B         2 ""     
#>  5 C         2 "Y"    
#>  6 C         3 ""     
#>  7 C         4 ""     
#>  8 D         4 "Y"    
#>  9 D         5 ""     
#> 10 E         1 "Y"    
#> 11 E         2 ""

Created on 2020-12-19 by the reprex package (v0.3.0)

1 Like

Thank you very much. it works well.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.