I haven't found a good solution for this question in a while. What I want to do is to average certain rows from a dataframe based on an ID and create a differnt dataframe. Let's say I have a dataframe that looks like this:
Data
structure(list(ID = c("A1", "A1", "A1", "A1", "A2", "A2", "A2"
), Name = c("S.coli", "S.coli", "S.coli", "S.coli", "S.coli", 
"S.coli", "S.coli"), Location = c("Indv1", "Indv1", "Indv1", 
"Indv1", "Indv2", "Indv2", "Indv2"), x1 = c(1L, 1L, 1L, 1L, 1L, 
1L, 1L), x2 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L), x3 = c(3L, 3L, 3L, 
3L, 3L, 3L, 3L), x4 = c(4L, 4L, 4L, 4L, 4L, 4L, 4L), x5 = c(5L, 
5L, 5L, 5L, 5L, 5L, 5L)), class = "data.frame", row.names = c(NA, 
-7L))
ID   Name     Location x1 x2 x3 x4 x5
A1   S.coli   Indv1     1  2  3  4   5
A1   S.coli   Indv1     1  2  3  4   5
A1   S.coli   Indv1     1  2  3  4   5
A1   S.coli   Indv1     1  2  3  4   5
A2   S.coli   Indv2     1  2  3  4   5
A2   S.coli   Indv2     1  2  3  4   5
A2   S.coli   Indv2     1  2  3  4   5
Now I want a second dataframe with the average per variable x per ID code also keeping the name and location. Dataframe of mean values:
ID   Name     Location x1 x2 x3 x4 x5
A1   S.coli   Indv1    1  2  3  4   5
A2   S.coli   Indv2    1  2  3  4   5
I have many ID codes so subsetting and then joining the tables is almost like doing manually. I was wondering if there is a more effective way to do it. Thank you in advance!!
 
     
    