代码之家  ›  专栏  ›  技术社区  ›  stevec Zxeenu

沿向量映射并输出数据框嵌套列表(或嵌套列表)列数据帧)使用purrr函数族?

  •  1
  • stevec Zxeenu  · 技术社区  · 4 年前

    最小可复制示例

    library(purrr)
    library(tidyverse)
    
    set.seed(123)
    input <- c(1, 2, 3)
    rand <- runif(3)
    
    
    data.frame(input=input) %>% 
      map_df(function(x) { x * rand}) 
    
    #   input
    #   <dbl>
    # 1 0.288
    # 2 1.58 
    # 3 1.23 
    

    1 回复  |  直到 4 年前
        1
  •  1
  •   nniloc    4 年前

    map mutate 在数据帧中创建列表。

    data.frame(input=input) %>% 
     mutate(rand_3 = map(input, function(x) x * rand))
    
    
    #------
      input                          rand_3
    1     1 0.2875775, 0.7883051, 0.4089769
    2     2 0.5751550, 1.5766103, 0.8179538
    3     3 0.8627326, 2.3649154, 1.2269308