I'm facing an issue in R which I have described below. I need the sum and count (avoiding NA's) of all columns per group ID.
What I have:
ID S1 S2
1 1 NA 1
2 1 5 2
3 1 1 3
4 2 2 7
5 3 4 NA
6 3 2 11
What I need
ID S1.sum S2.sum S1.count S2.count
1 1 6 6 2 3
2 2 2 7 1 1
3 3 6 11 2 1
Further edit: I have the exact same dataset but now there is an additional column. "T"
ID S1 S2 T
1 1 NA 1 3
2 1 5 2 3
3 1 1 3 3
4 2 2 7 5
5 3 4 NA 2
6 3 2 11 2
Is it possible to get the count and sum like before but now only when S1/2 fulfills the condition (S1/2 <= T);
The resulting dataframe would look like:
ID S1.sum S2.sum S1.count S2.count
1 1 1 6 1 3
2 2 2 0 1 0
3 3 2 0 1 0