-1

I have a data frame called 'whole_time' that looks something like this:

     1  2  3   4   5    6
F1   20 60 65 80  100  600
F2.  40 57 88 120 130  550
F3.  67 88 92 134 220  350  
F4   92 98 99 170 345  650

Basically I want to create a new column in 'whole_time' called 'MIN' that contains all the minimum values for each row, so an output of something like this:

     1  2  3   4   5    6.   MIN
F1   20 60 65 80  100  600.  20
F2.  40 57 88 120 130  550.  40
F3.  67 88 92 134 220  350   67
F4   92 98 99 170 345  650.  92

Then, from there I would like to find the MAX value from the 'MIN' column.

user438383
  • 5,716
  • 8
  • 28
  • 43
Kate
  • 31
  • 3
  • Please dont share images of data, what you have now is fine. – user438383 May 31 '23 at 22:06
  • You're probably looking for something like `mutate(mn = min(c_across(everything()))` but it's hard to say what specific column specification will be correct inside `c_across()` without a more reproducible data set. – joran May 31 '23 at 22:13
  • `do.call(pmin, whole_time)` for a parallel minimum, as per this old question for the maximum - https://stackoverflow.com/questions/26268402/return-pmin-or-pmax-of-data-frame-with-multiple-columns/26268477 , which also covers joran's dplyr option. – thelatemail May 31 '23 at 23:48

2 Answers2

0
df <- read.csv(textConnection('1,2,3,4,5,6
F1,20,60,65,80,100,600
F2.,40,57,88,120,130,550
F3.,67,88,92,134,220,350  
F4,92,98,99,170,345,650'))
df$MIN <- apply(df, 1, min)
print(df)
    X1 X2 X3  X4  X5  X6 MIN
F1  20 60 65  80 100 600  20
F2. 40 57 88 120 130 550  40
F3. 67 88 92 134 220 350  67
F4  92 98 99 170 345 650  92
br00t
  • 1,440
  • 8
  • 10
0

We can use rowMins() function from matrixStats package:

library(matrixStats)

df$MIN <- rowMins(as.matrix(df))
df

  X1 X2 X3  X4  X5  X6 MIN
F1  20 60 65  80 100 600  20
F2. 40 57 88 120 130 550  40
F3. 67 88 92 134 220 350  67
F4  92 98 99 170 345 650  92
TarJae
  • 72,363
  • 6
  • 19
  • 66