Home > Mobile >  selecting the column with the maximum value
selecting the column with the maximum value

Time:11-17

I have a dataframe in the wide format such as below:

Subject Volume.1 Volume.2 Volume.3 Volume.4
1 77 22 1 NA
2 65 182 NA NA
3 98 NA NA NA
4 66 76 145 677

I am wanting to select the volume.1 and the column and the largest volume of Volume1-4 irrespective of which column it came from but am struggling to code this correctly. Some of the columns are Na when a subject does not have a recording then.

For instance with the above example the table would look like:

Subject Volume.1 Worst volume
1 77 22
2 65 182
3 98 NA
4 66 677

I was wondering if anyone could help?

CodePudding user response:

We may use pmax

cbind(df[1:2], WorseVolume = do.call(pmax, c(df[3:5], na.rm = TRUE)))

-output

 Subject Volume.1 WorseVolume
1       1       77          22
2       2       65         182
3       3       98          NA
4       4       66         677

data

df <- structure(list(Subject = 1:4, Volume.1 = c(77L, 65L, 98L, 66L
), Volume.2 = c(22L, 182L, NA, 76L), Volume.3 = c(1L, NA, NA, 
145L), Volume.4 = c(NA, NA, NA, 677L)), class = "data.frame", row.names = c(NA, 
-4L))
  •  Tags:  
  • r
  • Related