Home > Net >  Check if select columns have the same value
Check if select columns have the same value

Time:12-30

I am new to this community and hoping to solve this problem.

I have a dataframe that looks as follows:

df <- data.frame(id=c(1,2,3,4),id2=c("x","y","z","a"),value1=c(1,2,3,4),value2=c(1,2,3,4),value3=c(1,1,3,4),value4=c(1,2,3,4),value5=c(1,2,3,"ab"))

I want to check if value1,value2, value3 and value4 are all the same. I am able to get this to work with the following code:

comp4 <- df[,3:7] %>%
rowwise %>%
mutate(same =n_distinct(unlist(cur_data())) == 1) %>%
ungroup

However, I end up losing the id1 and id2 columns in the data frame. How do I solve this by keeping id1 and id2.

CodePudding user response:

Instead of selecting only the columns, keep them while selecting the columns in across within mutate

library(dplyr)
df %>% 
   rowwise %>% 
   mutate(same = n_distinct(unlist(across(starts_with('value'), 
        ~ as.character(.x)))) == 1) %>%
   ungroup

-output

# A tibble: 4 × 8
     id id2   value1 value2 value3 value4 value5 same 
  <dbl> <chr>  <dbl>  <dbl>  <dbl>  <dbl> <chr>  <lgl>
1     1 x          1      1      1      1 1      TRUE 
2     2 y          2      2      1      2 2      FALSE
3     3 z          3      3      3      3 3      TRUE 
4     4 a          4      4      4      4 ab     FALSE

Or may also use if_all

df %>% 
   mutate(same = if_all(value2:value5, ~ .== value1))
  id id2 value1 value2 value3 value4 value5  same
1  1   x      1      1      1      1      1  TRUE
2  2   y      2      2      1      2      2 FALSE
3  3   z      3      3      3      3      3  TRUE
4  4   a      4      4      4      4     ab FALSE
  • Related