Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select specific/all columns in rowwise

Tags:

r

dplyr

I have the following table:

col1 col2 col3 col4
1 2 1 4
5 6 6 3

My goal is to find the max value per each row, and then find how many times it was repeated in the same row.

The resulting table should look like this:

col1 col2 col3 col4 max_val repetition
1 2 1 4 4 1
5 6 6 3 6 2

Now to achieve this, I am doing the following for Max:

df%>% rowwise%>%
mutate(max=max(col1:col4))

However, I am struggling to find the repetition. My idea is to use this pseudo code in mutate: sum( "select current row entirely or only for some columns"==max). But I don't know how to select entire row or only some columns of it and use its content to do the check, i.e.: is it equal to the max. How can we do this in dplyr?

like image 848
GitZine Avatar asked Mar 03 '26 04:03

GitZine


1 Answers

A dplyr approach:

library(dplyr)
df %>% 
  rowwise() %>% 
  mutate(max_val = max(across(everything())),
         repetition = sum(across(col1:col4) == max_val))

# A tibble: 2 × 6
# Rowwise: 
   col1  col2  col3  col4 max_val repetition
  <int> <int> <int> <int>   <int>      <int>
1     1     2     1     4       4          1
2     5     6     6     3       6          2

An R base approach:

df$max_val <- apply(df,1,max)
df$repetition <- rowSums(df[, 1:4] == df[, 5])
like image 157
Jilber Urbina Avatar answered Mar 04 '26 17:03

Jilber Urbina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!