-1

I'm currently stack with my crazy large dataset. I'd like to calculate a banal median in R, but I want it for a certain Layer and Zone. For instances, the median of column dC of each zone A and layer 0 -5. Anyone knows how to do it? Please, find attached the head of my dataset. Many thanks in advance. 1

2 Answers2

0

You can do this with aggregate. Here is a small example.

x = rnorm(100)
y = sample(LETTERS[1:3], 100, replace=TRUE)
z = sample(LETTERS[4:6], 100, replace=TRUE)

aggregate(x~y+z, FUN=median)
  y z           x
1 A D -0.11114506
2 B D -0.29459743
3 C D -0.26080279
4 A E -0.27316768
5 B E -0.44661497
6 C E -0.11971012
7 A F  0.36835509
8 B F -0.08730946
9 C F  0.08759923
G5W
  • 32,266
  • 10
  • 31
  • 60
0

Take a subset of the data frame rows that meet your filter conditions, and then take the median of that subset's dC column, e.g.:

> median(df[df$Layer == '0 - 5' && df$Zone == 'A1',]$dC)
Alex Reynolds
  • 91,635
  • 50
  • 223
  • 320
  • Be careful of [doubling the ampersand](https://stackoverflow.com/a/6559049/1422451) in R as you want a vector of TRUEs returned. – Parfait Aug 07 '18 at 02:02