【问题标题】:How to select, duplicate, and rename multiple columns in tibble with tidy evaluation semantics?如何使用整洁的评估语义选择、复制和重命名 tibble 中的多个列?
【发布时间】:2017-10-24 17:17:56
【问题描述】:

我想在我的 tibble 中复制一组变量,以便在下游评估中获得 variable_unmodifiedvariable 值。我想出了一个使用旧式下划线 NSE select_() 函数和 .dots 的 hacky 版本,但我想使用新的 NSE 整洁评估语义方法。

这就是我想要的:

tibble_to_max <- tibble(
  "a_col" = c("1", "2", "3", "4"),
  "max_1" = c("3;4", "2{3}4", "7", ".{1}"),
  "max_2" = c("3;4", "2{3}4", "7", ".{1}")
)

cols_to_max <- c("max_1", "max_2")

unparsed_names <-  paste0(cols_to_max, "_unparsed")

tibble_to_max %>%
  bind_cols(select_(., .dots = setNames(cols_to_max, unparsed_names)))

输出:

# A tibble: 4 x 5
  a_col max_1 max_2 max_1_unparsed max_2_unparsed
  <chr> <chr> <chr>          <chr>          <chr>
1     1   3;4   3;4            3;4            3;4
2     2 2{3}4 2{3}4          2{3}4          2{3}4
3     3     7     7              7              7
4     4  .{1}  .{1}           .{1}           .{1}

但是,如果我尝试使用 select()!! 进行操作,.dots 将无法正常工作:

tibble_to_max %>%
  bind_cols(select(., .dots = setNames(!!cols_to_max, !!unparsed_names)))

列未按要求命名:

# A tibble: 4 x 5
  a_col max_1 max_2 .dots1 .dots2
  <chr> <chr> <chr>  <chr>  <chr>
1     1   3;4   3;4    3;4    3;4
2     2 2{3}4 2{3}4  2{3}4  2{3}4
3     3     7     7      7      7
4     4  .{1}  .{1}   .{1}   .{1}

这样做的正确方法是什么?此外,避免将unparsed_names 定义为单独变量的奖励积分...

【问题讨论】:

    标签: r tidyverse nse


    【解决方案1】:

    可能是这样的

    您的数据

    tibble_to_max <- tibble(
      "a_col" = c("1", "2", "3", "4"),
      "max_1" = c("3;4", "2{3}4", "7", ".{1}"),
      "max_2" = c("3;4", "2{3}4", "7", ".{1}")
    )
    

    解决方案使用nest,然后一次性复制所有嵌套数据,然后unnest。我使用rename_all 重命名data_copy 中的列

    library(tidyverse)
    tibble_to_max %>%
      nest(-a_col) %>%
      mutate(data_copy = data) %>%
      mutate(data_copy = map(data_copy, ~.x %>% rename_all(funs(paste0(., "_unparsed"))))) %>% 
      unnest(data, data_copy)
    

    输出

    # A tibble: 4 x 5
      a_col max_1 max_2 max_1_unparsed max_2_unparsed
      <chr> <chr> <chr>          <chr>          <chr>
    1     1   3;4   3;4            3;4            3;4
    2     2 2{3}4 2{3}4          2{3}4          2{3}4
    3     3     7     7              7              7
    4     4  .{1}  .{1}           .{1}           .{1}
    

    【讨论】:

    • 这会得到我想要的输出 tibble 的值,但不会重命名它们。我认为这相当于这样做:tibble_to_max %&gt;% bind_cols(select(., !!cols_to_max))
    【解决方案2】:

    感谢@CPak 让我走上了正确的道路。这完成了我的目标,并使用整洁的评估语义而不是select_()

    tibble_to_max <- tibble(
      "a_col" = c("1", "2", "3", "4"),
      "max_1" = c("3;4", "2{3}4", "7", ".{1}"),
      "max_2" = c("3;4", "2{3}4", "7", ".{1}")
    )
    
    cols_to_max <- c("max_1", "max_2")
    
    tibble_to_max %>%
      bind_cols(
        select_at(., 
          .vars = !!cols_to_max, 
          .funs = funs(paste0(., "_unparsed"))
          )
        )
    

    【讨论】:

    • Error in !cols_to_max : invalid argument type。然后我将.vars = !!cols_to_max 更改为.vars = cols_to_max 并且成功了。
    猜你喜欢
    • 2019-02-28
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2021-02-25
    • 2020-04-28
    • 2023-03-30
    • 2017-11-16
    • 2021-01-08
    相关资源
    最近更新 更多