0

I want to find the max and min for each Gene in the following table. I know, that the following function gives the max (or min), but I could not manage to get both at the same time.

tapply(df$Value, df$Gene, max)

Appreciate!

Small test set:

df <- read.table(header = TRUE, text = 'Gene   Value
A      12
                 A      10
                 A      123
                 A      1
                 B      3
                 B      5
                 B      6
                 C      1
                 D      3
                 D      45
                 D      98
                 D      234
                 D      4')
Sotos
  • 51,121
  • 6
  • 32
  • 66
Ester Silva
  • 670
  • 6
  • 24

2 Answers2

3
range()

Is the function that returns both the max and the min

Here you'd do:

tapply(df$Value, df$Gene, range)

# $A
# [1]   1 123

# $B
# [1] 3 6

# $C
# [1] 1 1

# $D
# [1]   3 234
De Novo
  • 7,120
  • 1
  • 23
  • 39
0

You can keep using tapply, and simply modify the FUN argument to return multiple summary statistics. For example:

do.call(rbind, tapply(df$Value, df$Gene, FUN = function(x) c(max = max(x), min = min(x))))
#  max min
#A 123   1
#B   6   3
#C   1   1
#D 234   3
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68