【问题标题】:output two dataframes inside dplyr pipes [duplicate]在 dplyr 管道内输出两个数据帧[重复]
【发布时间】:2018-05-16 13:53:53
【问题描述】:

进行一些计算,我注意到为了进一步操作,我需要将数据帧保留在先前的状态。 所以,我有一堆 dplyr 步骤,在管道链的中间,我想将数据帧状态保存到其他对象中

这可能吗?

df <- read.csv(file) %>%
      mutate(....) %>%
      mutate(....) %>%
      # save df state to new df2 object here.......*****
      group_by(....) %>%
      arrange(var) %>%
      summary()

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    试试下面的代码:

    df <- read.csv(file) %>%
      mutate(....) %>%
      mutate(....)
    
    # save df state to new df2 object here.......*****
    df2 <- df
    
    df %>% group_by(....) %>%
      arrange(var) %>%
      summary()
    

    按要求在管道链内:

    df <- read.csv(file) %>%
      mutate(....) %>%
      mutate(....) %>%
      {df2 <<- .} %>% # save df state to new df2 object here.......*****
      group_by(....) %>%
      arrange(var) %>%
      summary()
    

    希望对你有帮助!

    【讨论】:

    • 这是我的第一种方法,但不是按要求“在管道内”
    猜你喜欢
    • 2018-09-30
    • 2012-10-17
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 2019-10-03
    • 2014-04-23
    • 2019-09-01
    相关资源
    最近更新 更多