【问题标题】:R append dataframe name to each of its columns within a list of dataframesR将数据框名称附加到数据框列表中的每个列
【发布时间】:2020-10-28 07:03:34
【问题描述】:

我想将数据框名称附加到以下数据框列表中的每一列:

df1 <- tibble(V1=c(1, 1, 3, 1),
                V2=c(2, 1, 2, 2),
                V3=c(4, 1, 1, 2))
df2 <- tibble(V1=c(1, 1, 3, 1),
                V2=c(2, 1, 2, 2),
                V3=c(4, 1, 1, 2))
df <- list(df1, df2)
names(df) <- c("df1", "df2")
df

这就是我想要的样子:

$df1
# A tibble: 4 x 3
  df1_V1 df1_V2 df1_V3
   <dbl>  <dbl>  <dbl>
1      1      2      4
2      1      1      1
3      3      2      1
4      1      2      2

$df2
# A tibble: 4 x 3
  df2_V1 df2_V2 df2_V3
   <dbl>  <dbl>  <dbl>
1      1      2      4
2      1      1      1
3      3      2      1
4      1      2      2

我试过了:

    appendDFnameToColumns <- function(df, prefix = "", sep = "") {
      colnames(df) <- paste(prefix, colnames(df), sep = sep) 
      df
      }
    
    df <- map(df, appendDFnameToColumns, 
                             prefix = names(df$.),
                             sep ="_")

提前致谢

【问题讨论】:

    标签: r


    【解决方案1】:

    purrr:

    library(tidyverse)
    names(df) %>% 
        map(function(dtf) {get(dtf) %>% rename_with(~paste0(dtf,"_",.))})
    [[1]]
    # A tibble: 4 x 3
      df1_V1 df1_V2 df1_V3
       <dbl>  <dbl>  <dbl>
    1      1      2      4
    2      1      1      1
    3      3      2      1
    4      1      2      2
    
    [[2]]
    # A tibble: 4 x 3
      df2_V1 df2_V2 df2_V3
       <dbl>  <dbl>  <dbl>
    1      1      2      4
    2      1      1      1
    3      3      2      1
    4      1      2      2
    

    【讨论】:

      【解决方案2】:

      tidyversepurrrs imap 的方法:

      library(dplyr)
      library(purrr)
      
      imap(df,~rename_with(.x, function(x) paste(.y, x, sep = '_')))
      
      #$df1
      # A tibble: 4 x 3
      #  df1_V1 df1_V2 df1_V3
      #   <dbl>  <dbl>  <dbl>
      #1      1      2      4
      #2      1      1      1
      #3      3      2      1
      #4      1      2      2
      
      #$df2
      # A tibble: 4 x 3
      #  df2_V1 df2_V2 df2_V3
      #   <dbl>  <dbl>  <dbl>
      #1      1      2      4
      #2      1      1      1
      #3      3      2      1
      #4      1      2      2
      

      或者在基础 R 中,您可以使用 Map

      Map(function(x, y) setNames(x, paste(y, names(x), sep = '_')), df, names(df))
      

      【讨论】:

        【解决方案3】:

        我们可以使用rename!!!

        library(dplyr)
        library(purrr)
        library(stringr)
        map2(df, names(df), ~ .x %>% 
               rename(!!! setNames(names(.), str_c(.y, names(.), sep="_"))))
        

        -输出

        #$df1
        # A tibble: 4 x 3
        #  df1_V1 df1_V2 df1_V3
        #   <dbl>  <dbl>  <dbl>
        #1      1      2      4
        #2      1      1      1
        #3      3      2      1
        #4      1      2      2
        
        #$df2
        # A tibble: 4 x 3
        #  df2_V1 df2_V2 df2_V3
        #   <dbl>  <dbl>  <dbl>
        #1      1      2      4
        #2      1      1      1
        #3      3      2      1
        #4      1      2      2
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-04
          • 2012-09-26
          • 2021-07-28
          • 2019-09-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多