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)]
rowwise maximum for R
Here's one possibility:
df$colMax <- apply(df, 1, function(x) max(x[x != 9]))
Finding row-wise maximum value column with distinct column indices in a numpy matrix
Assuming the minimum value in the matrix is 0
for i in range(len(arr)): # for the ith row
maxcol = np.argmax(arr[i])
arr[:,maxcol] = np.zeros( len(arr[:,maxcol]) )
out.append((i,maxcol))
This works by finding the index of the max column for a row (using argmax
), and then setting all of that column's elements to an arbitrary minimum value (0
here, given efficiently by np.zeros
)
This should work, I'm trying it out rn. And is definitely less than O(n^2)
but don't know exactly how much.
Edit: I tried it and out
is [(0, 0), (1, 1), (2, 3), (3, 2), (4, 0)]
. So it does have the desired output, but an extra term (4,0). [which is there because iterates over all rows].
If you want to iterate till you 'run out' of columns, you can use min(arr.shape)
instead of len(arr)
Time Complexity
We're running a for
loop once, that takes O(n)
Inside the for loop, we have np.argmax
, which is also O(n)
Then comes the replacements which are constant time [O(1)
]
So it's not that optimised. You can write your own code for that, by directly coding the edge cases and other improvements like checking only the required parts, etc...
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
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
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 to calculate max and min of multiple columns (row wise) using awk
You may use this awk
:
awk 'BEGIN{FS=OFS=","} NR==1 {print $0, "New_col"; next} {print $0, ($2 > $4 ? $2 : $4) - ($3 < $5 ? $3 : $5)}' df.csv
col1,col2,col3,col4,col5,New_col
A,2,5,7,9,2
B,6,10,2,3,3
C,3,4,6,8,2
A more readable version:
awk '
BEGIN { FS = OFS = "," }
NR == 1 {
print $0, "New_col"
next
}
{
print $0, ($2 > $4 ? $2 : $4) - ($3 < $5 ? $3 : $5)
}' df.csv
Related Topics
Directly Creating Dummy Variable Set in a Sparse Matrix in R
Promise Already Under Evaluation: Recursive Default Argument Reference or Earlier Problems
Inst and Extdata Folders in R Packaging
How to Parametrize Function Calls in Dplyr 0.7
Find the N Most Common Values in a Vector
How to Access the Help/Documentation .Rd Source Files in R
R Extract Rows Where Column Greater Than 40
Why Is Message() a Better Choice Than Print() in R for Writing a Package
Text Clustering with Levenshtein Distances
How to Convert Data.Frame to Transactions for Arules
R - How to Get Row & Column Subscripts of Matched Elements from a Distance Matrix
Set Certain Values to Na with Dplyr
R: Assign Variable Labels of Data Frame Columns
Extract a Column from a Data.Table as a Vector, by Position
Create End of the Month Date from a Date Variable
In R, Use Gsub to Remove All Punctuation Except Period