How to merge and average data?

Hi,
I have a following problem. I have two datasets, lets say with values A and B.

I would like to create a third dataset with values C, where C = (A + B)/2

Could you please help me? I dont know how to write the code in R

Thanks!

A simple tidyverse solution would rely on dplyr's mutate function, for example below


library(dplyr)

df <- tibble(
  a = 1:4,
  b = 4:7
)

df <- df %>% 
  mutate(
    c = (a + b) / 2
  )
df
#> # A tibble: 4 x 3
#>       a     b     c
#>   <int> <int> <dbl>
#> 1     1     4   2.5
#> 2     2     5   3.5
#> 3     3     6   4.5
#> 4     4     7   5.5

Created on 2019-02-20 by the reprex package (v0.2.1)

Created on 2019-02-20 by the reprex package (v0.2.1)

I'd encourage you to take a good intro to R and/or the tidyverse to get familiar with data manipulation topics like this. You can good vignettes or check out the cheatsheets.

Garret also made a nice webinar on data wrangling here.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.