-1

For simplicity, lets say I have the following data frame:

name value
a      5
a      3
b      5
c      9
…    …
z      12

where values in column name are duplicates or unique and may/may not share the same value, how would it be possible to to find the average value of each letter in regards to duplicates?

1 Answers1

0

Using dplyr might be easiest, combining group by and summarize:

library(dplyr)

df1 <- data.frame(name = c('a', 'a', 'a', 'b', 'b', 'c'), 
                  value = c(1, 4, 6, 2, 5, 7), 
                  stringsAsFactors = F)  

df1 %>%
  group_by(name) %>%
  summarize(avg_value = mean(value)) %>%
  ungroup()

#> # A tibble: 3 × 2
#>   name  avg_value
#>   <chr>     <dbl>
#> 1 a          3.67
#> 2 b          3.5 
#> 3 c          7
VvdL
  • 2,799
  • 1
  • 3
  • 14