This is my first time asking a question here and I'm a beginner at R.
I have a huge dataset, where I want to get some overview of the values of multiple columns, based on category:
sampleID|category|element_1|element_2|element_3|element_4|
----------------------------------------------------------
1 | A | 12.53 | 46.17 | 94.09 | 25.23 |
2 | B | 19.53 | 16.17 | 14.09 | 28.23 |
3 | C | 21.53 | 56.17 | 24.09 | 26.23 |
4 | D | 18.53 | 96.17 | 34.09 | 21.23 |
5 | B | 17.53 | 76.17 | 44.09 | 24.23 |
6 | A | 32.53 | 36.17 | 54.09 | 25.23 |
What I've been trying to do is get a mean of each element by each category, what I've been mostly trying are things around tapply
function in R:
tapply(data$element1, data$category, mean)
This gives me nice results for one element column, but I cannot seem to find an answer how to do that on all columns, without doing it on each column of elements by hand (mean of element1, element2, element3 etc. by category).
What I want is this:
category | element_1| element_2| element_3
A | mean | mean | mean
B | mean | mean | mean
C | mean | mean | mean
I've tried versions of apply
and aggregate
, but cannot get it to work.
Any advice is appreciated, if I need to supply more information, please let me know!