I'm trying to calculate sum or a row in R divided by group number and apply that on every subject (n = 102). For example, in the excel sheet, I'm trying to calculate the inclination of choosing organic food in the fruits category (organic = 1, non-organic = 0). To calculate this, I added row of banana + apple answers together and divide that by 2, then apply the same formula on the rest of the subject. I could do this in excel by using the function SUM(X1:X2)/2. How do I do this in R?
I have attached an image in the below.
If I understood correctly, you want to sum row-wise and divide each sum by the current row number, is that it?
If you just want to divide by the number of groups (which I assume you're referring to number of columns in your data), then just do: