Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min for each row in a data frame

Tags:

r

max

min

I'm try to calculate minimum across multiple columns (row-wise min) in a data frame, but the min function automatically returns the minimum across the whole of each column rather than for each row separately. I'm sure I'm missing something really simple here? Any ideas much appreciated.

x <- c(1,2,7)
y <- c(1,5,4)
minIwant <- c(1,2,4)
df <- data.frame(x, y, minIwant)
df$minIget <- min(df$x,df$y)
df
  x y minIwant minIget
1 1 1        1       1
2 2 5        2       1
3 7 4        4       1
like image 334
Rob Avatar asked Jan 08 '16 17:01

Rob


People also ask

How do you calculate minimum row?

Row wise min of the dataframe in R using dplyr: Method 1 takes up the columns 2 to 4 and performs the row wise operation with NA values replaced to zero. row wise min is calculated using pipe (%>%) operator of the dplyr package.

How do I find the minimum value of a row in R?

min() in R The min() is a built-in R function that takes an object as an input and returns the minimum value out of it. To find the minimum value of vector elements, data frame, and columns, use the min() function.

How do you find the minimum value of a data frame?

To find minimum value of every row in DataFrame just call the min() member function with DataFrame object with argument axis=1 i.e. It returned a series with row index label and minimum value of each row.

How do you find the minimum value in pandas?

Pandas DataFrame min() MethodThe min() method returns a Series with the minimum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the minimum value for each row.


4 Answers

You can use apply to go through each row

apply(df, 1, FUN = min)

Where 1 means to apply FUN to each row of df, 2 would mean to apply FUN to columns.

To remove missing values, use:

apply(df, 1, FUN = min, na.rm = TRUE)
like image 120
nico Avatar answered Oct 20 '22 16:10

nico


We could use pmin, which finds the parallel minima of sets of values. Since our df is technically a list, we will need to run it via do.call.

df$min <- do.call(pmin, df)

which gives

df
#   x y min
# 1 1 1   1
# 2 2 5   2
# 3 7 4   4

Data:

df <- data.frame(x = c(1, 2, 7), y = c(1, 5, 4))

Furthermore, if na.rm = TRUE is needed, you can do

df$min <- do.call(pmin, c(df, na.rm = TRUE))
like image 39
Rich Scriven Avatar answered Oct 20 '22 17:10

Rich Scriven


Just want to add on how you can also do this with dplyr.

library(dplyr)

x<-c(1,2,7)
y<-c(1,5,4)
df <- data.frame(x,y)

df %>% rowwise() %>% mutate(minIget = min(x, y))
# A tibble: 3 x 3
    x     y   minIget
  <dbl> <dbl>   <dbl>
1    1.    1.      1.
2    2.    5.      2.
3    7.    4.      4.
like image 5
Nik Muhammad Naim Avatar answered Oct 20 '22 17:10

Nik Muhammad Naim


We could also use rowMins from library(matrixStats)

library(matrixStats)
df$minIwant <- rowMins(as.matrix(df))
like image 4
akrun Avatar answered Oct 20 '22 17:10

akrun