0

Very sorry if this is a reposted question, I checked the search engine and couldn't find the answer I was looking for. Say I have the following dataset:

Plot Plant Count
1   101     1     9
2   101     2    15
3   101     3     5
4   101     4    15
5   101     5    26
6   102     1     9
7   102     2    26
8   102     3     9
9   102     4    15
10  102     5    17
11  103     1    12
12  103     2     6
13  103     3    22
14  103     4    12
15  103     5     6

I'd like to average the "Count" number between the 5 plants of each plot. However, in my real dataset, I have much more than 3 plots. Is there a way to write my code so that it automatically averages all my plots at once? I'd like to learn to write a code that would get me the average for each plot as efficiently as possible. Any help would be very much appreciated.

I am fairly new to stackoverflow and am not the strongest with R, so if I have made a mistake in my formatting or something similar please let me know. Thanks for your time!

Tom
  • 377
  • 3
  • 13

1 Answers1

0

Try this with dplyr using group_by() and summarise(). Here the code:

library(dplyr)
#Data
newdf <- df %>% group_by(Plot) %>% summarise(Avg=mean(Count))

Output:

# A tibble: 3 x 2
   Plot   Avg
  <int> <dbl>
1   101  14  
2   102  15.2
3   103  11.6

Some data used:

#Data
df <- structure(list(Plot = c(101L, 101L, 101L, 101L, 101L, 102L, 102L, 
102L, 102L, 102L, 103L, 103L, 103L, 103L, 103L), Plant = c(1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Count = c(9L, 
15L, 5L, 15L, 26L, 9L, 26L, 9L, 15L, 17L, 12L, 6L, 22L, 12L, 
6L)), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"))

If you want to keep your variables use mutate() in this way:

#Code
newdf <- df %>% group_by(Plot) %>% mutate(Avg=mean(Count))

Output:

# A tibble: 15 x 4
# Groups:   Plot [3]
    Plot Plant Count   Avg
   <int> <int> <int> <dbl>
 1   101     1     9  14  
 2   101     2    15  14  
 3   101     3     5  14  
 4   101     4    15  14  
 5   101     5    26  14  
 6   102     1     9  15.2
 7   102     2    26  15.2
 8   102     3     9  15.2
 9   102     4    15  15.2
10   102     5    17  15.2
11   103     1    12  11.6
12   103     2     6  11.6
13   103     3    22  11.6
14   103     4    12  11.6
15   103     5     6  11.6

Or using base R:

#Base R
newdf <- aggregate(Count~Plot,data=df,mean)

Output:

  Plot Count
1  101  14.0
2  102  15.2
3  103  11.6
Duck
  • 39,058
  • 13
  • 42
  • 84