Find the maximum and minimum value of every column and then find the maximum and minimum value of every row
Figured it out.
Minimum and maximum of every column:
apply(a,2,min)
apply(a,2,max)
Minimum and maximum of every row:
apply(a,1,min)
apply(a,1,max)
Found the information here http://www.personality-project.org/r/r.commands.html
How to find the max, min value of ALL Dataframe [ not values by column neither rows ]
You can try:
max_val = df.max().max()
How can I find max and min value of every row
You initialize minValue
and maxValue
with temperature[i][0]
before writing anything into that position. Instead use the input value to initialize it:
cin >> temperature[i][0];
int maxValue = temperature[i][0];
int minValue = temperature[i][0];
for (int j = 1; j < numberOfDays; j++) // start at 1
{
cin >> temperature[i][j];
if (temperature[i][j] > maxValue)
maxValue = temperature[i][j];
if (temperature[i][j] < minValue)
minValue = temperature[i][j];
}
Fastest way to find the maximum minimum value of two 'connected' matrices
Perhaps you could re-evaluate how you look at the problem in context of what min and max actually do. Say you have the following concrete example:
>>> np.random.seed(1)
>>> print(A := np.random.randint(10, size=(4, 5)))
[[5 8 9 5 0]
[0 1 7 6 9]
[2 4 5 2 4]
[2 4 7 7 9]]
>>> print(B := np.random.randint(10, size=(5, 3)))
[[1 7 0]
[6 9 9]
[7 6 9]
[1 0 1]
[8 8 3]]
You are looking for a pair of numbers in A
and B
such that the column in A
is the same as the row of B
, and the you get the maximum smaller number.
For any set of numbers, the largest pairwise minimum happens when you take the two largest numbers. You are therefore looking for the max in each column of A
, row of B
, the minimum of those pairs, and then the maximum of that. Here is a relatively simple formulation of the solution:
candidate_i = A.argmax(axis=0)
candidate_k = B.argmax(axis=1)
j = np.minimum(A[candidate_i, np.arange(A.shape[1])], B[np.arange(B.shape[0]), candidate_k]).argmax()
i = candidate_i[j]
k = candidate_k[j]
And indeed, you see that
>>> i, j, k
(0, 2, 2)
>>> A[i, j]
9
>>> B[j, k]
9
If there are collisions, argmax
will always pick the first option.
How to find the maximum and minimum for specific columns in each row
Try the following:
df[, "Max"] <- apply(df[, 5:length(df)], 1, max, na.rm = TRUE)
df[, "Min"] <- apply(df[, 5:length(df)], 1, min, na.rm = TRUE)
Find minimum value in an array that is larger than another column in R
You can use the following solution.
- Here c(...) refers to all variables in each row of your data set and I chose only those that starts with
attack
- Then I chose only those values that are greater than the corresponding value of
hosp
in each row and since you were looking for the first one that is greater than the value ofhosp
I usedfirst
function to extract that ..2
also refers to the value of the second variablehosp
in each row
library(dplyr)
library(purrr)
data %>%
mutate(afterh = pmap_dbl(., ~ {x <- c(...)[3:5];
first(sort(x[x > ..2]))}))
id hosp attack1 attack2 attack3 out afterh
1 100 3 1 4 5 7 4
2 105 5 6 7 10 12 6
3 108 2 3 9 NA 11 3
4 200 6 4 10 NA 12 10
5 205 2 1 NA NA 9 NA
As an alternative as mentioned by dear Mr. @Greg in a very large data set, we can use min
function in place of first(sort))
combination to ensure a faster evaluation time of the following solution. In case there is no value greater than hosp
like in the last row min
function would return Inf
so I made sure that it would return the value 0
instead you can change it with the value you prefer:
data %>%
mutate(afterh = pmap_dbl(., ~ {x <- c(...)[3:5];
out <- min(x[x > ..2], na.rm = TRUE);
if(!is.finite(out)) 0 else out}))
id hosp attack1 attack2 attack3 out afterh
1 100 3 1 4 5 7 4
2 105 5 6 7 10 12 6
3 108 2 3 9 NA 11 3
4 200 6 4 10 NA 12 10
5 205 2 1 NA NA 9 0
How to find out minimum value from various columns in data frame with R?
Using dplyr
:
library(dplyr)
cols <- grep('Diff', names(df), value = TRUE)
df %>%
group_by(Accountid) %>%
mutate(Min = min(c_across(cols)),
Min_name = cols[which.min(c_across(cols))]) %>%
select(Accountid, Min, Min_name)
# Accountid Min Min_name
# <int> <int> <chr>
#1 101 1000 Diff1
#2 201 100 Diff1
#3 301 1000 Diff3
#4 401 400 Diff3
#5 501 12000 Diff2
data
df <- structure(list(Accountid = c(101L, 201L, 301L, 401L, 501L),
Fcast1 = c(4000L, 2900L, -100L, 5000L, 9000L), Fcast2 = c(2000L, 3300L, 5500L,
8000L, 12000L), Fcast3 = c(1000L, 5000L, -800L, 7100L, 2000L),
Diff1 = c(1000L, 100L, 1700L, 2500L, 15000L), Diff2 = c(3000L,
300L, 7300L, 500L, 12000L), Diff3 = c(4000L, 2000L, 1000L,
400L, 22000L)), class = "data.frame", row.names = c(NA, -5L))
Find a maximum value for multiple entries in a column
We may use max.col
to get the index of the year column and use that to subset the value
i1 <- grep("^Yr\\d+", names(df))
i2 <- max.col(df[i1], "first")
yr_name <- names(df)[i1][i2]
value <- df[i1][cbind(seq_len(nrow(df)), i2)]
out <- data.frame(df['Sector'], yr_name, value)
Finding maximum and minimum value in a matrix column using Octave
Given a matrix X
, max(X)
will return the maximum value in each column. You can index the result to get the value for a given column:
max(X)(1) % max of the fist column (doesn't work in MATLAB)
Alternatively, extract the column and get its max:
max(X(:,1)) % max of the fist column
max
(and many similar functions) operate on columns by default. To get the maximum of each row, use max(X,[],2)
.
Fastest way to find the maximum minimum value of three 'connected' matrices
We can either brute force it using numpy
broadcasting or try a bit of smart branch cutting:
import numpy as np
def bf(A,B,C):
I,J = A.shape
J,K = B.shape
return np.unravel_index((np.minimum(np.minimum(A[:,:,None],C[:,None,:]),B[None,:,:])).argmax(),(I,J,K))
def cut(A,B,C):
gmx = min(A.min(),B.min(),C.min())
I,J = A.shape
J,K = B.shape
Y,X = np.unravel_index(A.argsort(axis=None)[::-1],A.shape)
for y,x in zip(Y,X):
if A[y,x] <= gmx:
return gamx
curr = np.minimum(B[x,:],C[y,:])
camx = curr.argmax()
cmx = curr[camx]
if cmx >= A[y,x]:
return y,x,camx
if gmx < cmx:
gmx = cmx
gamx = y,x,camx
return gamx
from timeit import timeit
I = 100
J = 150
K = 200
for rep in range(4):
print("trial",rep+1)
A = np.random.rand(I, J)
B = np.random.rand(J, K)
C = np.random.rand(I, K)
print("results identical",cut(A,B,C)==bf(A,B,C))
print("brute force",timeit(lambda:bf(A,B,C),number=2)*500,"ms")
print("branch cut",timeit(lambda:cut(A,B,C),number=10)*100,"ms")
It turns out that at the given sizes branch cutting is well worth it:
trial 1
results identical True
brute force 169.74265850149095 ms
branch cut 1.951422297861427 ms
trial 2
results identical True
brute force 180.37619898677804 ms
branch cut 2.1000938024371862 ms
trial 3
results identical True
brute force 181.6371419990901 ms
branch cut 1.999850495485589 ms
trial 4
results identical True
brute force 217.75578951928765 ms
branch cut 1.5871295996475965 ms
How does the branch cutting work?
We pick one array (A, say) and sort it from largest to smallest. We then go through the array one by one comparing each value to the appropriate values from the other arrays and keeping track of the running maximum of minima. As soon as the maximum is no smaller than the remaining values in A we are done. As this will typically happen rather soonish we get a huge saving.
Related Topics
Plotting Cumulative Counts in Ggplot2
Conditionally Display Block of Markdown Text Using Knitr
Problems Using Foreach Parallelization
How to Suppress Row Names When Using Dt::Renderdatatable in R Shiny
R: Robust Se's and Model Diagnostics in Stargazer Table
How to Specify Command Line Parameters to R-Script in Rstudio
Change the Color of the Axis Labels
How to Manually Fill Colors in a Ggplot2 Histogram
Replace Characters in Column Names Gsub
Geom_Boxplot() from Ggplot2:Forcing an Empty Level to Appear
Geom_Point() and Geom_Line() for Multiple Datasets on Same Graph in Ggplot2
Creating a Facet_Wrap Plot with Ggplot2 with Different Annotations in Each Plot
How to Use Plyr to Number Rows
Wrap Long Text in Kable Table Column
Linear Regression and Storing Results in Data Frame
Ggplot Legend Issue W/ Geom_Point and Geom_Text
Is Data Really Copied Four Times in R's Replacement Functions
Error When Using Predict() on a Randomforest Object Trained with Caret's Train() Using Formula