Create table with mean of columns from two different files

Macintosh; Intel Mac OS X 12_4_0

Hi, I have two files, one named mint_04_export and one named maxt_04_export:

> dput(mint_04_export)

structure(list(CodeNidif = c("A08B08_200801", "A08E31_201501",
"A09A03_200901", "A12B01_201301"), Altitude = c(296, 278, 301,
296), 091-095 = c(-9.82, -9.73, -9.84, -9.82), 096-100 = c(-9.78,
-9.75, -9.82, -9.78), 101-105 = c(-6.96, -6.9, -6.99, -6.96
)), row.names = c(NA, 4L), class = "data.frame")

> dput(maxt_04_export)
structure(list(CodeNidif = c("A08B08_200801", "A08E31_201501",
"A09A03_200901", "A12B01_201301"), Altitude = c(296, 278, 301,
296), 091-095 = c(1.71, 1.78, 1.68, 1.71), 096-100 = c(1.17,
1.26, 1.13, 1.17), 101-105 = c(5.52, 5.59, 5.48, 5.52)), row.names = c(NA,
4L), class = "data.frame")

I want to create a table where I would keep the first columns : CodeNifid and Altitude, but would like to get the mean of the columns of the same name. E.g.
the value of 091-095 of the row of A08B08_200801 from both both tables mint_04_export and maxt_04_export.

I am not able to do this, here is what I tried (by joining the two files together):

> combin_04<-mint_04 %>%
>   left_join(maxt_04, by='CodeNidif')
> 
> moyt_04<-mint_04 %>%
>   left_join(maxt_04, by='CodeNidif') %>%
>   group_by(CodeNidif) %>%
>   summarize(moyt=mean(`091-095.x`,`091-095.y`))

Below is one way to achieve the desired output.

library(tidyverse)

combin_04 = bind_rows(mint_04, maxt_04) %>%
  group_by(CodeNidif, Altitude) %>%
  summarise_at(1:3, ~mean(.)) %>%
  ungroup()

combin_04
#> # A tibble: 4 × 5
#>   CodeNidif     Altitude `091-095` `096-100` `101-105`
#>   <chr>            <dbl>     <dbl>     <dbl>     <dbl>
#> 1 A08B08_200801      296     -4.06     -4.30    -0.72 
#> 2 A08E31_201501      278     -3.98     -4.24    -0.655
#> 3 A09A03_200901      301     -4.08     -4.35    -0.755
#> 4 A12B01_201301      296     -4.06     -4.30    -0.72

Created on 2023-02-09 with reprex v2.0.2.9000

This topic was automatically closed 7 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.