Reputation: 1457
I have a data frame like this:
Expt Replicate
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
C 4
I want to return the number of replicates for each experiment. Like this:
Expt #Reps
A 3
B 3
C 4
This has got to be super simple, but I have tried some things like
ddply(df, Expt, .fun=max(Replicate))
with no luck. Please help.
Upvotes: 1
Views: 3298
Reputation: 115392
A data.table
solution (to counting the number of replicates)
library(data.table)
df <- data.table(df)
df[,list(max = .N),by='Expt']
If you want the maximum a column then use
df[,list(max = max(Replicate)),by='Expt']
Upvotes: 4
Reputation: 61154
Another way to achieve your goal is
> df <- data.frame(Expt = rep(c('A', 'B', 'C'), c(3,3,4)), Replicate = append(rep(1:3, 3), 4, 10))
> tapply(df$Replicate, df$Expt, max)
A B C
3 3 4
But if you really want to use ddply
, you can try:
library(plyr)
ddply(df,.(Expt),numcolwise(max))
Upvotes: 2
Reputation: 1457
Thanks everyone. I also found this works:
aggregate(as.numeric(df$Replicate), list(exptCount=df$expt),max)
Upvotes: 3
Reputation: 529
another simple way:
summary(df[,1]) #where df is your data frame and you want the 1st column counts ("Expt")
Note: the 1st column is a factor
and this applies to any columns which are factor
Upvotes: -1
Reputation: 28492
table
is your friend. You even need not second column, table
will count items in first column for you:
> x <- c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C')
> x
[1] "A" "A" "A" "B" "B" "B" "C" "C" "C" "C"
> table(x)
x
A B C
3 3 4
Upvotes: 4