-3

I am searching for an efficient and fast way to do the following: I have a data frame with, say, 2 variables, A and B, where the values for A can occur several times:

mat<-data.frame('VarA'=rep(seq(1,10),2),'VarB'=rnorm(20))

VarA        VarB
1         0.95848233
2        -0.07477916
3         2.08189370
4         0.46523827
5         0.53500190
6         0.52605101
7        -0.69587974
8        -0.21772252
9         0.29429577
10        3.30514605
1         0.84938361
2         1.13650996
3         1.25143046

Now I want to get a vector giving me for every unique value of VarA

unique(mat$VarA)

the maximum of VarB conditional on VarA. In the example here that would be

1    0.95848233
2    1.13650996
3    2.08189370
etc...

My data-frame is very big so I want to avoid the use of loops.

Stefan Voigt
  • 209
  • 2
  • 5
  • 18

3 Answers3

1

Try to use data.table package.

library(data.table)
mat <- data.table(mat)
result <- mat[,max(VarB),VarA]
print(result)
Joswin K J
  • 690
  • 1
  • 7
  • 16
1

Try this:

library(dplyr)
mat %>% group_by(VarA) %>%
summarise(max=max(VarB))
Wyldsoul
  • 1,468
  • 10
  • 16
1

Try this:

library(plyr)
ddply(mat, .(VarA), summarise, VarB=min(VarB))
Prradep
  • 5,506
  • 5
  • 43
  • 84