13

My data looks like:

head(myframe)
      id fwt_r fwt_l
[1,] 101    72    52
[2,] 102    61    48
[3,] 103    46    49
[4,] 104    48    41
[5,] 105    51    42
[6,] 106    49    35

I want to select the greater of the two values among fwt_r and fwt_l. I want the output like:

72
61
49
48
51
49

Kindly help me out. Thanks!

thelatemail
  • 91,185
  • 12
  • 128
  • 188
anmol_b
  • 133
  • 1
  • 1
  • 7

1 Answers1

30

You are looking for the 'pmax' function

Just run this:

pmax(myframe$fwt_r, myframe$fwt_l)

pmax means 'parallel maxima' (or vectorized)

agenis
  • 8,069
  • 5
  • 53
  • 102
  • 2
    Equivalently, `with(myframe, pmax(fwt_r, fwt_l))` – Matthew Lundberg Feb 15 '15 at 22:29
  • Is there a function that does this but with absolute values? I have two vectors which I want to combine. The first vector has values in the first 1:N rows and zero in N+1:2N and the next column has zeroes from 1:N and values in rows N+1:2N. I want to merge so that I have no zeroes and only get the values from each column – Emil Krabbe May 11 '21 at 11:40
  • @EmilKrabbe in your particular case, summing the two columns would get your desired result no? – agenis May 11 '21 at 12:27
  • sum adds all the elements into a single number. I need element wise addition though. – Emil Krabbe May 11 '21 at 12:48
  • yeah i mean like `df$a + df$b` – agenis May 11 '21 at 12:49
  • 1
    I figured it out. I just use `rowSums(cbind(df$Col1,df$Col2))` thank you for the talk – Emil Krabbe May 11 '21 at 12:52