Home > Net >  Average by row of multiple dataframes in r
Average by row of multiple dataframes in r

Time:06-07

I have 3 dataframes:

dput(head(nSOS_VI_GPR_05,20))
structure(list(ID = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "P0", "P01", "P02", 
"P1", "P13", "P18", "P19", "P2"), N_D_SOS = c(129, 349, 256, 
319, 306, 128, 309, 244, 134, 356, 131, 302, 276, 296, 294, 310, 
295, 337, 295, 291), N_EVI_SOS = c(134, 337, 212, 331, 296, 134, 
309, 334, 112, 352, 108, 302, 260, 303, 295, 287, 648, 384, 424, 
283), N_NDVI_SOS = c(7, 302, 259, 300, 297, 139, 302, 346, 111, 
358, 137, 298, 269, 303, 296, 289, 484, 328, 436, 287), N_NIRv_SOS = c(133, 
297, 266, 285, 293, 130, 312, 335, 115, 363, 140, 302, 264, 306, 
298, 290, 645, 340, 423, 293), N_kNDVI_SOS = c(132, 314, 268, 
319, 297, 139, 308, 334, 111, 356, 135, 301, 271, 294, 297, 296, 
419, 331, 436, 294)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

> dput(head(nSOS_VI_GPR_10,20))
structure(list(ID = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "P0", "P01", "P02", 
"P1", "P13", "P18", "P19", "P2"), N_D_SOS = c(129, 349, 256, 
319, 306, 128, 309, 244, 134, 356, 131, 302, 276, 296, 294, 310, 
295, 337, 295, 291), N_EVI_SOS = c(139, 342, 271, 336, 339, 141, 
316, 338, 119, 362, 144, 308, 267, 317, 304, 293, 657, 406, 428, 
290), N_NDVI_SOS = c(1, 314, 266, 317, 307, 143, 306, 350, 118, 
363, 144, 303, 274, 309, 302, 294, 487, 339, 440, 293), N_NIRv_SOS = c(139, 
334, 271, 327, 341, 139, 318, 339, 124, 370, 149, 308, 271, 319, 
306, 296, 655, 382, 427, 302), N_kNDVI_SOS = c(137, 335, 272, 
325, 319, 144, 314, 340, 119, 362, 143, 305, 277, 306, 303, 300, 
425, 349, 440, 299)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

> dput(head(nSOS_VI_GPR_15,20))
structure(list(ID = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "P0", "P01", "P02", 
"P1", "P13", "P18", "P19", "P2"), N_D_SOS = c(129, 349, 256, 
319, 306, 128, 309, 244, 134, 356, 131, 302, 276, 296, 294, 310, 
295, 337, 295, 291), N_EVI_SOS = c(144, 347, 274, 340, 346, 148, 
322, 341, 125, 370, 151, 313, 272, 329, 312, 297, 665, 423, 431, 
295), N_NDVI_SOS = c(6, 331, 269, 322, 316, 146, 310, 353, 124, 
367, 149, 306, 278, 313, 306, 298, 490, 348, 443, 297), N_NIRv_SOS = c(143, 
338, 274, 331, 347, 154, 324, 343, 130, 376, 155, 313, 276, 328, 
313, 301, 662, 419, 430, 308), N_kNDVI_SOS = c(141, 343, 275, 
328, 332, 148, 319, 344, 124, 366, 149, 309, 281, 313, 307, 303, 
429, 374, 443, 303)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

I want to calculate the average of the columns N_EVI_SOS, N_NDVI_SOS, N_NIRv_SOS and N_kNDVI_SOS of the 3 dataframes by row.

For example, the first row of the column N_EVI_SOS would be average (134; 139; 144) = 139. In the end, I would have 20 averages for each of the correspondent columns.

I've looking for a solution where I don't have to join the dataframes. Any help will be much appreciated.

CodePudding user response:

tidyvrse

l <- mget(x = ls(pattern = "nSOS_VI_GPR_"))

VARS <- c("N_EVI_SOS", "N_NDVI_SOS", "N_NIRv_SOS")

library(tidyverse)

l_VARS <- map(VARS, function(x) map_dfc(l, function(y) y[x]))

map_dfc(l_VARS, rowMeans) %>% 
  set_names(nm = paste0(VARS, "_mean"))

result

#> # A tibble: 20 × 3
#>    N_EVI_SOS_mean N_NDVI_SOS_mean N_NIRv_SOS_mean
#>             <dbl>           <dbl>           <dbl>
#>  1           139             4.67            138.
#>  2           342           316.              323 
#>  3           252.          265.              270.
#>  4           336.          313               314.
#>  5           327           307.              327 
#>  6           141           143.              141 
#>  7           316.          306               318 
#>  8           338.          350.              339 
#>  9           119.          118.              123 
#> 10           361.          363.              370.
#> 11           134.          143.              148 
#> 12           308.          302.              308.
#> 13           266.          274.              270.
#> 14           316.          308.              318.
#> 15           304.          301.              306.
#> 16           292.          294.              296.
#> 17           657.          487               654 
#> 18           404.          338.              380.
#> 19           428.          440.              427.
#> 20           289.          292.              301

data

nSOS_VI_GPR_05 <- structure(list(ID = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
                      "LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "P0", "P01", "P02", 
                      "P1", "P13", "P18", "P19", "P2"), N_D_SOS = c(129, 349, 256, 
                                                                    319, 306, 128, 309, 244, 134, 356, 131, 302, 276, 296, 294, 310, 
                                                                    295, 337, 295, 291), N_EVI_SOS = c(134, 337, 212, 331, 296, 134, 
                                                                                                       309, 334, 112, 352, 108, 302, 260, 303, 295, 287, 648, 384, 424, 
                                                                                                       283), N_NDVI_SOS = c(7, 302, 259, 300, 297, 139, 302, 346, 111, 
                                                                                                                            358, 137, 298, 269, 303, 296, 289, 484, 328, 436, 287), N_NIRv_SOS = c(133, 
                                                                                                                                                                                                   297, 266, 285, 293, 130, 312, 335, 115, 363, 140, 302, 264, 306, 
                                                                                                                                                                                                   298, 290, 645, 340, 423, 293), N_kNDVI_SOS = c(132, 314, 268, 
                                                                                                                                                                                                                                                  319, 297, 139, 308, 334, 111, 356, 135, 301, 271, 294, 297, 296, 
                                                                                                                                                                                                                                                  419, 331, 436, 294)), row.names = c(NA, -20L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                           "tbl", "data.frame"))


nSOS_VI_GPR_10 <- structure(list(ID = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
                      "LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "P0", "P01", "P02", 
                      "P1", "P13", "P18", "P19", "P2"), N_D_SOS = c(129, 349, 256, 
                                                                    319, 306, 128, 309, 244, 134, 356, 131, 302, 276, 296, 294, 310, 
                                                                    295, 337, 295, 291), N_EVI_SOS = c(139, 342, 271, 336, 339, 141, 
                                                                                                       316, 338, 119, 362, 144, 308, 267, 317, 304, 293, 657, 406, 428, 
                                                                                                       290), N_NDVI_SOS = c(1, 314, 266, 317, 307, 143, 306, 350, 118, 
                                                                                                                            363, 144, 303, 274, 309, 302, 294, 487, 339, 440, 293), N_NIRv_SOS = c(139, 
                                                                                                                                                                                                   334, 271, 327, 341, 139, 318, 339, 124, 370, 149, 308, 271, 319, 
                                                                                                                                                                                                   306, 296, 655, 382, 427, 302), N_kNDVI_SOS = c(137, 335, 272, 
                                                                                                                                                                                                                                                  325, 319, 144, 314, 340, 119, 362, 143, 305, 277, 306, 303, 300, 
                                                                                                                                                                                                                                                  425, 349, 440, 299)), row.names = c(NA, -20L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                           "tbl", "data.frame"))

nSOS_VI_GPR_15 <- structure(list(ID = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
                      "LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "P0", "P01", "P02", 
                      "P1", "P13", "P18", "P19", "P2"), N_D_SOS = c(129, 349, 256, 
                                                                    319, 306, 128, 309, 244, 134, 356, 131, 302, 276, 296, 294, 310, 
                                                                    295, 337, 295, 291), N_EVI_SOS = c(144, 347, 274, 340, 346, 148, 
                                                                                                       322, 341, 125, 370, 151, 313, 272, 329, 312, 297, 665, 423, 431, 
                                                                                                       295), N_NDVI_SOS = c(6, 331, 269, 322, 316, 146, 310, 353, 124, 
                                                                                                                            367, 149, 306, 278, 313, 306, 298, 490, 348, 443, 297), N_NIRv_SOS = c(143, 
                                                                                                                                                                                                   338, 274, 331, 347, 154, 324, 343, 130, 376, 155, 313, 276, 328, 
                                                                                                                                                                                                   313, 301, 662, 419, 430, 308), N_kNDVI_SOS = c(141, 343, 275, 
                                                                                                                                                                                                                                                  328, 332, 148, 319, 344, 124, 366, 149, 309, 281, 313, 307, 303, 
                                                                                                                                                                                                                                                  429, 374, 443, 303)), row.names = c(NA, -20L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                           "tbl", "data.frame"))
  • Related