I had to rewrite the data.frame file (for future use, insert the dput results because we hate overwriting your data), but here is my attempt. I assume that you are looking for something along the lines of an aggregate function:
df <- data.frame(id = as.factor(c(51,51,51,52,52,53,53,53,53,53,54,54,54)), pace = c("(T)","(T)","(T)","(T)","(R)","(T)","(T)","(R)","(R)","(R)","(T)","(T)","(T)"), type = c("(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(BC)","(BC)","(BC)"), value = c("(L)","(L)","(H)","(H)","(H)","(L)","(H)","(H)","(H)","(H)","<blank>","<blank>","<blank>"), abundance = c(0,0,0,0,0,1,1,1,1,1,0,0,0)) smallnames <- colnames(do.call("cbind",as.list(aggregate(cbind(value, pace, abundance) ~ id + type, data = lapply(df, as.character), table)))) smallnames [1] "id" "type" "(H)" "(L)" "<blank>" "(R)" "(T)" "0" [9] "1" df.new <- do.call("data.frame", as.list(aggregate(cbind(value, pace, abundance) ~ id + type, data = lapply(df, as.character), table))) colnames(df.new) <- smallnames df.new$abundance <- df.new$`1` df.new id type (H) (L) <blank> (R) (T) 0 1 abundance 1 54 (BC) 0 0 3 0 3 3 0 0 2 51 (JC) 1 2 0 0 3 3 0 0 3 52 (JC) 2 0 0 1 1 2 0 0 4 53 (JC) 4 1 0 3 2 0 5 5 df.final <- df.new[, -which(colnames(df.new) %in% c("<blank>","0","1"))] df.final id type (H) (L) (R) (T) abundance 1 54 (BC) 0 0 0 3 0 2 51 (JC) 1 2 0 3 0 3 52 (JC) 2 0 1 1 0 4 53 (JC) 4 1 3 2 5
Let me know if this is what you are looking for, or if you have a problem with it.