I am trying to obtain the max value row wise across several columns, and create 2 new columns with the max value, and the corresponding column name. Then, using the column name, I need to select the value of another column that shares a substring of that column name.
This is an example of what I trying to solve:
measure_day1 <- c(1,2,5)
measure_day2 <- c(5,7,1)
measure_day3 <- c(2,3,9)
temp_day1 <- c(25, 27, 29)
temp_day2 <- c(31, 33, 35)
temp_day3 <- c(14, 16, 19)
df <- data.frame(measure_day1, measure_day2, measure_day3, temp_day1, temp_day2, temp_day3)
measure_day1 measure_day2 measure_day3 temp_day1 temp_day2 temp_day3
1 1 5 2 25 31 14
2 2 7 3 27 33 16
3 5 1 9 29 35 19
And this would be the result:
measure_day1 measure_day2 measure_day3 temp_day1 temp_day2 temp_day3 measure_max day_measure_max temp_day_measure_max
1 1 5 2 25 31 14 5 measure_day2 31
2 2 7 3 27 33 16 7 measure_day2 33
3 5 1 9 29 35 19 9 measure_day3 19
I found this similar question but could not complete my task.
For each row return the column name of the largest value
Any help is greatly appreciated.
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 usingfilter
. Usingpivot_wider()
you can have the desired values and then the filter for max values is applied. Finally you can merge to your original data usingleft_join()
and the id you created based on rows. Here the code:Output: