【问题标题】:renaming all dataframe columns with stringr and dplyr使用 stringr 和 dplyr 重命名所有数据框列
【发布时间】:2018-07-19 14:47:06
【问题描述】:

我正在尝试使用dplyrstringr 重命名我的数据框中的所有列,但它似乎没有按照我想要的方式工作。我应该如何更改以下代码以获得我想要的输出(如下代码所示)?

这是完全可重现的代码:

library(dplyr)
library(stringr)
library(tibble)
library(rlang)

# dataframe
x <-
  tibble::as.tibble(cbind(
    Grace_neu_wrong = c(1:4),
    Grace_acc_wrong = c(1:4),
    Grace_att_wrong = c(1:4),
    Grace_int_wrong = c(1:4)
  ))

# defining custom function to rename the entire dataframe in a certain way
string_conversion <- function(df, ...) {

  # preparing the dataframe
  df <- dplyr::select(.data = df,
                      !!rlang::quo(...))

  # custom function to split the name of each column in a certain way
  splitfn <- function(x) {
    x <- as.character(x)
    split <- stringr::str_split(string = x, pattern = "_")[[1]]
    paste(split[2], split[3], '_', split[1], sep = '')
  }

  # applying the splitfn function to each column name and outputting the data frame
  df_new <- df %>%
    dplyr::select_all(.funs = colnames) %>%
    dplyr::mutate_all(.funs = splitfn)

  return(df_new)

}

# the output I get
string_conversion(df = x, names(x))
#> # A tibble: 4 x 4
#>   Grace_neu_wrong Grace_acc_wrong Grace_att_wrong Grace_int_wrong
#>   <chr>           <chr>           <chr>           <chr>          
#> 1 NANA_1          NANA_1          NANA_1          NANA_1         
#> 2 NANA_1          NANA_1          NANA_1          NANA_1         
#> 3 NANA_1          NANA_1          NANA_1          NANA_1         
#> 4 NANA_1          NANA_1          NANA_1          NANA_1

# the output I desire
tibble::as.tibble(cbind(
  neuwrong_Grace = c(1:4),
  accwrong_Grace = c(1:4),
  attwrong_Grace = c(1:4),
  intwrong_Grace = c(1:4)
))
#> # A tibble: 4 x 4
#>   neuwrong_Grace accwrong_Grace attwrong_Grace intwrong_Grace
#>            <int>          <int>          <int>          <int>
#> 1              1              1              1              1
#> 2              2              2              2              2
#> 3              3              3              3              3
#> 4              4              4              4              4

reprex package (v0.1.1.9000) 于 2018 年 2 月 8 日创建。

【问题讨论】:

    标签: r dplyr tidyverse stringr


    【解决方案1】:

    您可以在一行中执行此操作,而无需使用 mutate,它应该用于列值而不是列名。相反,请使用 stringr::str_replace 和正则表达式执行以下操作。

    1. "(.*)_(.*)_(.*)" 模式是由下划线分隔的三组字符。
    2. 我们只需替换 "\\2\\3_\\1",即第 2 组,然后是第 3 组,然后是下划线,然后是第 1 组,从而得到我们想要的结果。

    因此代码只有一行:

    names(x) <- str_replace(names(x), "(.*)_(.*)_(.*)", "\\2\\3_\\1")
    print(x)
    # A tibble: 4 x 4
      neuwrong_Grace accwrong_Grace attwrong_Grace intwrong_Grace
               <int>          <int>          <int>          <int>
    1              1              1              1              1
    2              2              2              2              2
    3              3              3              3              3
    4              4              4              4              4
    

    【讨论】:

    • 非常感谢。非常优雅的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 2020-10-10
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多