Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to select the greater of the two values from two columns in R [duplicate]

Tags:

r

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!

like image 833
anmol_b Avatar asked Feb 15 '15 22:02

anmol_b


People also ask

How do you find the maximum of two columns in R?

Maximum value of a column in R can be calculated by using max() function. Max() Function takes column name as argument and calculates the maximum value of that column. Maximum of single column in R, Maximum of multiple columns in R using dplyr.

How do I select more than one column in R?

To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.


1 Answers

You are looking for the 'pmax' function

Just run this:

pmax(myframe$fwt_r, myframe$fwt_l)

pmax means 'parallel maxima' (or vectorized)

like image 70
agenis Avatar answered Nov 15 '22 14:11

agenis