relatively new to R and am trying to solve an issue with my syntax.
I have a dataframe that looks something like this:
Sample | Motility | Diet | Life.Position | n | t1 | t2 | t3 | … |
---|---|---|---|---|---|---|---|---|
0 m | Slow moving | Carnivore | Infaunal | 1 | 3 | 1 | 5 | … |
0 m | Fac. mobile | Filter feeder | Epifaunal | 2 | 1 | 1 | 3 | … |
0 m | Stationary, attached | Suspension feeder | Epifaunal | 1 | 3 | 4 | 2 | … |
0 m | Fac. mobile | Filter feeder | Deep infaunal | 2 | 4 | 1 | 3 | … |
0.5 m | Slow moving | Carnivore | Infaunal | 1 | 3 | 1 | 4 | … |
0.5 m | Fac. mobile | Filter feeder | Epifaunal | 2 | 0 | 1 | 1 | … |
0.5 m | Stationary, attached | Suspension feeder | Epifaunal | 1 | 3 | 0 | 3 | … |
0.5 m | Fac. mobile | Filter feeder | Deep infaunal | 2 | 4 | 2 | 3 | … |
In reality, I have 18 different samples and I have 100 different t columns that represent subsampled abundance data for species appearing in my test samples.
I have tried a couple of things, though due to my inexperience likely not the correct way or the most efficient way.
I am trying to take all 100 subsamples and sum the same traits (motility, diet, life.position) within each column so I get this result:
Sample | Motility | n | t1 | t2 | t3 | … |
---|---|---|---|---|---|---|
0 m | Slow moving | 1 | 3 | 1 | 5 | … |
0 m | Fac. mobile | 4 | 5 | 2 | 6 | … |
0 m | Stationary, attached | 1 | 3 | 4 | 2 | … |
0.5 m | Slow moving | 1 | 3 | 1 | 3 | … |
0.5 m | Fac. mobile | 4 | 4 | 3 | 4 | … |
0.5 m | Stationary, attached | 1 | 3 | 0 | 3 | … |
For example:
df %>%
group_by(Sample, Motility) %>%
summarise(trial = sum(across(t1:t100)))
This gave me 1 column with sums of each trait (nice) but summing all 100 trials together (not nice). I understand that a for loop might be able to help me out however my understanding of them and their functionality is extremely poor.
Sample | Motility | trial |
---|---|---|
0 m | Slow moving | 60 |
0 m | Fac. mobile | 35 |
0.5 m | Slow moving | 42 |
0.5 m | Fac. mobile | 26 |
0.5 m | Stationary, attached | 48 |
Any more information that you may need I can absolutely provide.
Thank you
ricoratso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.