代码之家  ›  专栏  ›  技术社区  ›  user113156

在列表上映射并更改以应用函数

r
  •  2
  • user113156  · 技术社区  · 5 年前

    我有一些数据,我试图映射并执行一些计算。其中一个列表如下:

    [[6]]
    # A tibble: 6 x 8
       var1  var2  var3  var4  mean    sd   min   max
      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    1    30    16    27  74.7  39.1  21.0     1   165
    2    28    12    18  74.3  39.1  21.0     1   165
    3    25     8    12  73.8  39.1  21.0     1   165
    4    33    13    20  73.4  39.1  21.0     1   165
    5    48    29    32  73.0  39.1  21.0     1   165
    6    59    37    47  72.6  39.1  21.0     1   165
    

    names .

    names <- c("var1", "var2", "var3", "var4")
    

    map 并将此函数应用于 姓名 .

    Scale_Me <- function(x){
      (x - min) / (max - min)
    }
    

    scaled_data <- map(
      dat, ~mutate_at(
        .,
        vars(matches(paste(names, collapse = "|"))),
        .funs = c("Scale_Me")
        )
      )
    

    预期产出将是:

    # A tibble: 6 x 8
       var1  var2  var3  var4  mean    sd   min   max var1_scale var2_scale
      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    1    30    16    27  74.7  39.1  21.0     1   165    xxx1     yyy1
    2    28    12    18  74.3  39.1  21.0     1   165    xxx2     yyy2
    3    25     8    12  73.8  39.1  21.0     1   165    xxx3     yyy3
    4    33    13    20  73.4  39.1  21.0     1   165    ...      ...
    5    48    29    32  73.0  39.1  21.0     1   165    
    6    59    37    47  72.6  39.1  21.0     1   165    xxxN     yyyN
    

    例如,这适用于一个变量 var1 :

    map(
      dat, ~mutate(.,
        var1_scaled = (var1 - min) / (max - min) 
      )
    )
    

    数据:

    dat <- list(structure(list(var1 = c(16, 52, 61, 56, 46, 30), var2 = c(7, 
    28, 30, 42, 31, 16), var3 = c(17, 36, 41, 41, 35, 27), var4 = c(76.7710873995529, 
    76.3531164480543, 75.935145496561, 75.5171745450677, 75.0992035935744, 
    74.6812326420812), mean = c(39.1029174452609, 39.1029174452609, 
    39.1029174452609, 39.1029174452609, 39.1029174452609, 39.1029174452609
    ), sd = c(21.0129393923035, 21.0129393923035, 21.0129393923035, 
    21.0129393923035, 21.0129393923035, 21.0129393923035), min = c(1, 
    1, 1, 1, 1, 1), max = c(165, 165, 165, 165, 165, 165)), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        var1 = c(52, 61, 56, 46, 30, 28), var2 = c(28, 30, 42, 31, 
        16, 12), var3 = c(36, 41, 41, 35, 27, 18), var4 = c(76.3531164480543, 
        75.935145496561, 75.5171745450677, 75.0992035935744, 74.6812326420812, 
        74.2632616905879), mean = c(39.1063703943161, 39.1063703943161, 
        39.1063703943161, 39.1063703943161, 39.1063703943161, 39.1063703943161
        ), sd = c(21.008257789887, 21.008257789887, 21.008257789887, 
        21.008257789887, 21.008257789887, 21.008257789887), min = c(1, 
        1, 1, 1, 1, 1), max = c(165, 165, 165, 165, 165, 165)), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        var1 = c(61, 56, 46, 30, 28, 25), var2 = c(30, 42, 31, 16, 
        12, 8), var3 = c(41, 41, 35, 27, 18, 12), var4 = c(75.935145496561, 
        75.5171745450677, 75.0992035935744, 74.6812326420812, 74.2632616905879, 
        73.8452907390946), mean = c(39.0972317671854, 39.0972317671854, 
        39.0972317671854, 39.0972317671854, 39.0972317671854, 39.0972317671854
        ), sd = c(21.0078807907002, 21.0078807907002, 21.0078807907002, 
        21.0078807907002, 21.0078807907002, 21.0078807907002), min = c(1, 
        1, 1, 1, 1, 1), max = c(165, 165, 165, 165, 165, 165)), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        var1 = c(56, 46, 30, 28, 25, 33), var2 = c(42, 31, 16, 12, 
        8, 13), var3 = c(41, 35, 27, 18, 12, 20), var4 = c(75.5171745450677, 
        75.0992035935744, 74.6812326420812, 74.2632616905879, 73.8452907390946, 
        73.4273197876013), mean = c(39.083515262499, 39.083515262499, 
        39.083515262499, 39.083515262499, 39.083515262499, 39.083515262499
        ), sd = c(21.0046980738339, 21.0046980738339, 21.0046980738339, 
        21.0046980738339, 21.0046980738339, 21.0046980738339), min = c(1, 
        1, 1, 1, 1, 1), max = c(165, 165, 165, 165, 165, 165)), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        var1 = c(46, 30, 28, 25, 33, 48), var2 = c(31, 16, 12, 8, 
        13, 29), var3 = c(35, 27, 18, 12, 20, 32), var4 = c(75.0992035935744, 
        74.6812326420812, 74.2632616905879, 73.8452907390946, 73.4273197876013, 
        73.009348836108), mean = c(39.065243711307, 39.065243711307, 
        39.065243711307, 39.065243711307, 39.065243711307, 39.065243711307
        ), sd = c(21.0044169232859, 21.0044169232859, 21.0044169232859, 
        21.0044169232859, 21.0044169232859, 21.0044169232859), min = c(1, 
        1, 1, 1, 1, 1), max = c(165, 165, 165, 165, 165, 165)), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        var1 = c(30, 28, 25, 33, 48, 59), var2 = c(16, 12, 8, 13, 
        29, 37), var3 = c(27, 18, 12, 20, 32, 47), var4 = c(74.6812326420812, 
        74.2632616905879, 73.8452907390946, 73.4273197876013, 73.009348836108, 
        72.5913778846148), mean = c(39.053170538267, 39.053170538267, 
        39.053170538267, 39.053170538267, 39.053170538267, 39.053170538267
        ), sd = c(21.0039330348987, 21.0039330348987, 21.0039330348987, 
        21.0039330348987, 21.0039330348987, 21.0039330348987), min = c(1, 
        1, 1, 1, 1, 1), max = c(165, 165, 165, 165, 165, 165)), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame")))
    
    1 回复  |  直到 5 年前
        1
  •  3
  •   akrun    5 年前

    我们可以在 list 具有 map 并应用于多个列 mutate_at

    library(dplyr)
    library(purrr)
    map(dat, ~ 
              .x %>%
                    mutate_at(vars(names), list( scaled = ~ (.- min)/(max - min))))
    

    如果我们需要使用 Scale_Me ,它需要将列名作为参数传入,或者指定 .data

    Scale_Me <- function(.data, x){
       (x - .data[["min"]]) / (.data[["max"]] - .data[["min"]])
      }
    
    map(dat, ~ 
              {tmp <- .x
              tmp %>%
                    mutate_at(vars(names), list( scaled = ~Scale_Me(.data = tmp, .)))})