Rowwise Maximum for R

rowwise maximum for R

Here's one possibility:

df$colMax <- apply(df, 1, function(x) max(x[x != 9]))

Calculate row-wise maximum

I won't vouch for its speed, but this at least avoids coercing to a matrix:

data[,mymax:=do.call(pmax,.SD)]

In R: row wise return max value and corresponding column name

Try this tidyverse approach. It can be more practical reshaping data to long previously creating an id per row and then extract the desired values using filter. Using pivot_wider() you can have the desired values and then the filter for max values is applied. Finally you can merge to your original data using left_join() and the id you created based on rows. Here the code:

library(dplyr)
library(tidyr)
#Code
newdf <- df %>% mutate(id=1:n()) %>%
left_join(df %>% mutate(id=1:n()) %>%
pivot_longer(-id) %>%
separate(name,c('Var','Day'),sep='_') %>%
pivot_wider(names_from=Var,values_from=value) %>%
group_by(id) %>%
filter(measure==max(measure)) %>%
mutate(Day=paste0('measure_',Day)) %>% select(-measure) %>%
rename(measure_max=Day,temp_day_measure_max=temp)) %>% select(-id)

Output:

  measure_day1 measure_day2 measure_day3 temp_day1 temp_day2 temp_day3  measure_max temp_day_measure_max
1 1 5 2 25 31 14 measure_day2 31
2 2 7 3 27 33 16 measure_day2 33
3 5 1 9 29 35 19 measure_day3 19

Finding the maximum value for each row among 3 columns in R

You can use the apply function for this like so:

df$max<-apply(X=df, MARGIN=1, FUN=max)

The MARGIN=1 argument indicated that for every row in X you wish to apply the function in FUN. If you use MARGIN=2 it will be by column or MARGIN=c(1,2) it will be both rows and columns.

Calculate the rowwise mean when a maximum number of NA values is given for a set of columns using dplyr

We can use across to select column of interest.

library(dplyr)

dat %>%
mutate(mean = ifelse(rowSums(is.na(across(-colA))) > 2,
NA,
rowMeans(across(-colA), na.rm = T)))

# A tibble: 4 × 6
colA colB colC colD colE mean
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 2 3 4 5 3.5
2 2 3 NA 4 5 4
3 3 NA NA NA 4 NA
4 4 NA NA 5 6 5.5

How can I get row-wise max based on condition of specific column in R dataframe?

Throw this function in an apply family function

func <- function(x) {
first.val <- x[1]
if (first.val < 5) {
return(max(x[2:(first.val+)])
} else {
return(max(x[2:6]))
}
}

Your desired output should be obtained by:

apply(data, 1, function(x) func(x)) #do it by row by setting arg2 = 1

Calculate the maximum value across all rows without manually typing the names of every column

You get some data:

m <- tibble(matrix(runif(1000 * 500), ncol = 500))

Make sure every column is a double, then this should ideally work:

m_with_max_col <- m %>% 
rowwise() %>%
mutate(max = max(c_across(where(is.numeric))))

This also works, but might be less desirable:

m_with_max_col <- m %>% 
rowwise() %>%
mutate(max = max(across()))

Solution is taken from : Row-wise operations

Filter rows with rowwise max value more than threshold value

You can use pmax, i.e.

x[do.call(pmax, x[-1]) >= 0.8,]
# Names A B C D
#2 name2 0.1 0.4 0.90 0.1
#3 name3 0.8 0.3 0.05 0.3

Column index with row-wise maximum value

You could use max.col() on the topic columns. If df is the data, try

max.col(df[grepl("^Topic", names(df))])
# [1] 3 3 2 3 3 3

So to add a new column MaxPct, we can do

df$MaxPct <- max.col(df[grepl("^Topic", names(df))])


Related Topics



Leave a reply



Submit