mutate case_when and combined with or

I want to create a new variable based on two conditions, for which the second condition may take two alternative values. Is there a way in the case_when function to combine "and" with "or" in one line of code? Say this is the first dataframe: df1 <- data.frame(company = c("A", "B", "C", "D"), since =… Read More mutate case_when and combined with or

Custom Sort in R

I have the following data frame in R. # Create a data frame with duplicates df <- data.frame( pagePath = c( "/page7", "/page9", "/page3", "/page9", "/page5", "/page7", "/page4", "/page9", "/page9", "/page4", "/page5", "/page7" ), country = c( "USA", "Canada", "UK", "Germany", "France", "India", "Australia", "Japan", "Brazil", "India", "South Africa", "Canada" ) ) I want to… Read More Custom Sort in R

R DPLYR Rowwise SUM

I have DATA = data.frame(GROUP = c(1,2,1,2,1,2), TIME = c(1,1,2,2,3,3), SCORE = c(0,7,9,8,3,4)) and seek to create WANT = data.frame(GROUP = c(1,2,3,1,2,3,1,2,3), TIME = c(1,1,1,2,2,2,3,3,3), SCORE = c(0,7,7,9,8,17,3,4,7)) Where for each value of TIME I sum up the SCORE values for all GROUP I try without success, WANT = DATA %>% group_by(TIME) %>% rowwise() %>%… Read More R DPLYR Rowwise SUM

Use `across()` to create new columns with a function of multiple other columns

Is it possible to create multiple new columns based on a pattern of column names, and a function of multiple column values? For example set.seed(123) tibble(x = seq(1:10), bm_a = runif(10), val_a = runif(10), bm_b = runif(10), val_b = runif(10)) |> mutate(rel_a = val_a – bm_a, rel_b = val_b – bm_b) # A tibble: 10… Read More Use `across()` to create new columns with a function of multiple other columns

Identify index presentations and re-attendances within a 28 day period

My dataset records presentations to a location by an individual. This is tabulated below but included as a dput further down. Identifier date "A1" "28/01/2020" "A1" "01/04/2020" "A1" "16/08/2020" "A1" "20/08/2020" "A1" "30/08/2020" "A1" "31/10/2020" "A1" "14/11/2020" "A1" "26/11/2020" "A1" "25/12/2020" "A1" "04/05/2021" "A1" "08/05/2021" "A1" "26/07/2021" The individual attends sporadically and on occasions returns… Read More Identify index presentations and re-attendances within a 28 day period