I have a data frame like the following:
a b1 b2 b3 b4 b5 b6 b7 b8 b9
D 4 6 9 5 3 9 7 9 8
F 7 3 8 1 3 1 4 4 3
R 2 5 5 1 4 2 3 1 6
D 9 2 1 4 3 3 8 2 5
D 5 4 3 1 6 4 1 8 3
R 3 7 9 1 8 5 3 4 2
D 4 1 8 2 6 3 2 7 5
F 7 1 7 2 7 1 6 2 4
D 6 3 9 3 9 9 7 1 2
The function tapply(df[,2], INDEX = df$a, sum)
works fine to produce a table that sums everything in df[,2] by df$a, but when I try tapply(df[,2:10], INDEX = df$a, sum)
to get a similar table, except with a sum for each column (2, 3, 4,..., 10), I get an error message reading:
Error in tapply(df[, 2:10], INDEX = df$a, sum) : arguments must have same length
Additionally, I would like the row names of the table to be the column names of df[,2:10]
, such that row 1 is b1, row 2 is b2, and row 9 is b9.