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
  • 47,396
  • 5
  • 31
  • 61
Ester Silva
  • 638
  • 6
  • 22

2 Answers2

1
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
  • 6,260
  • 21
  • 37
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
  • 45,165
  • 4
  • 35
  • 59