0

I have a data frame as below:

Nucleotides pos_1 pos_2 pos_3 pos_4 pos_5 pos_6 pos_7 pos_8 pos_9 pos_10 pos_11 pos_12 pos_13 pos_14 pos_15
1           A   839  1344  1151  1047  1145   770  1185  1048   782   1326    806    897    895    961    960
2           C   410   511   834  1072   688   695   836   884   643    865    853   1025    697    719    790
3           G   147  1313   955  1074  1262  1131   880   873   749    746   1260    751    771   1136   1219
4           T  2573   801  1029   776   874  1373  1068  1164  1795   1032   1050   1296   1606   1153   1000
  pos_16 pos_17 pos_18 pos_19 pos_20
1   1183    802    955    712    568
2   1017    843    582    429    581
3    712    722    925   1399   1779
4   1057   1602   1507   1429   1041

I want to have the maximum value of each column as a final data frame. So basically the final df should have a single row (regardless of Nucleotides) which includes max values for each column.

Apex
  • 1,055
  • 4
  • 22

1 Answers1

1

Easy with apply().

data("mtcars")
df = data.frame( var = names(mtcars), max_values = apply(mtcars, 2, max), row.names = NULL )
> df
    var max_values
1   mpg     33.900
2   cyl      8.000
3  disp    472.000
4    hp    335.000
5  drat      4.930
6    wt      5.424
7  qsec     22.900
8    vs      1.000
9    am      1.000
10 gear      5.000
11 carb      8.000

make sure you use apply() in conjunction with max() on numeric columns only.

Francesco Grossetti
  • 1,555
  • 9
  • 17
  • Thanks, but can I have in a single row? – Apex Apr 01 '20 at 14:37
  • Nope, unless you want the result as a vector. A `data.frame` always has a tabular structure. If you want to have a vector, that is what `apply()` returns by default. Just do `apply(mtcars, 2, max)`. – Francesco Grossetti Apr 01 '20 at 14:42