I would like to use the tapply() function on a dataframe, grouping the rows with the indexing. My problem is that the argument I would pass to the function is not a single column, but a pair of columns. This beacause the 2 columns of the data frame represent x-y points, which are intended as couples. Running tapply(dataframe , indexes , function) gives me the error that indexes has length different from tapply. How can I solve this? Thank you!
If there are more than one column to be summarised, use aggregate
instead of tapply
(as tapply
works for a single column)
aggregate(.~ indexes, transform(df1, indexes = indexes), FUN = yourfun)
Or another option is by
by(df1, list(indexes), FUN = yourfun)
Or it may be more flexible with tidyverse
library(dplyr)
df1 %>%
group_by(indexes) %>%
summarise(across(c(x, y), yourfun), .groups = 'drop')
Using a small reproducible example
indexes = rep(1:2, c(3, 2))
by(mtcars[1:5, 1:5], indexes, FUN = sum)