0

Taking some generic data

set.seed(123)
A <- sample(1997:2000, 50, replace=TRUE)
B <- sample(1:3, 50, replace=TRUE)
C <- rnorm(50)
df <- data.frame(A,B,C)

I want to create a summary table that for each A value (year), and then each B value (type), will print the corresponding median C value

I have tried the following code

in reference to this website

mytable <- table(df$A, df$B, median(df$C))
ftable(mytable)

and here

require(plyr)
ddply(df, .(A), summarize,
      type=unique(B),
      median=median(C))

but neither seems to work. The desired output should look similar to this: enter image description here

Community
  • 1
  • 1
lukeg
  • 1,327
  • 3
  • 10
  • 27

2 Answers2

0

Alternative solution using data.table package:

library(data.table)
dt <- data.table(df)
dt[, .(median = median(C)), by = c("A", "B")][order(A, B)]
Andriy T.
  • 2,020
  • 12
  • 23
0

Simple solution using dplyr would be :

df %>%
    group_by(A,B)%>%
    summarise(C = median(C))

Read above code like :
1st group by A and B, then summarise C by median of each group.

Atish
  • 174
  • 3
  • 9