根据 dplyr 与另一个数据框的匹配更改数据框中的列名

Posted

技术标签:

【中文标题】根据 dplyr 与另一个数据框的匹配更改数据框中的列名【英文标题】:Change column names in dataframe based on matching to another dataframe by dplyr 【发布时间】:2020-05-23 14:03:10 【问题描述】:

我有一个数据框,我想通过匹配另一个数据框来更改列名。

具有数据和列名的示例数据框:

df <- data.frame("Gene_Symbol" = c("Gene1","Gene2","Gene3","Gene4","Gene5","Gene6","Gene7"), 
             "Sample1" = c(85657.97656,54417.78906,110949.3281,53197.45313,87156.80469,NA,23880.2832), 
             "Sample2" = c(10423.40918,41660.73047,40094.54688,49519.78125,129387.1094,NA,23903.25977), 
             "Sample3" = c(18778.68359,43655.79688,NA,57447.08984,113266.1484,44810.26172,26316.6543), 
             "Sample4" = c(23919.53125,47829.02344,NA,51478.58203,116275.3359,43110.94922,25417.45508), 
             "Sample5" = c(NA,46677.20313,63389.45313,48722.15234,NA,77135.52344,40265.6875), 
             "Sample6" = c(NA,68596.22656,56802.60938,44712.64063,NA,47744.17969,33689.62891), 
             "Sample7" = c(NA,80506.14844,48722.99219,38629.00781,NA,37885,36638.02344))

我想用来从 Sample 交换到上面 df 中的 Tumor 编号的 datframe。

df2 <- data.frame("Sample_name" = c("Sample1","Sample2","Sample3","Sample4","Sample5","Sample6", "Sample7"), "Tumor_name" = c("Tumor56", "Tumor17", "Tumor99", "Tumor2", "Tumor34", "Tumor84", "Tumor51"))

我在 dplyr 中找到了一个方法,见下文,但感觉非常精细。有没有更简单的方法?

library(tidyverse)
df %>% 
  column_to_rownames("Gene_Symbol")%>%                    # Bring Gene_Symbol to row name before transpose
  t()%>%                                                  # Transpose to be able to use join
  data.frame()%>%                                         # Transpose makes a matrix - make dataframe again
  rownames_to_column("Sample_name")%>%                    # Bring sample names to column to use join
  left_join(., df2, by = "Sample_name", copy = TRUE) %>%  # Join by Sample_name column in both data sets
  column_to_rownames("Tumor_name")%>%                     # Bring Tumor names to row name before transpose
  select(-Sample_name)%>%                                 # Drop Sample name column 
  t()%>%                                                  # Transpose 
  data.frame()%>%                                         # Transpose makes a matrix - make dataframe again 
  rownames_to_column("Gene_Symbol")                       # Transfer rownames to column again

匹配交换名称会很好,因为我可以预见我需要对列名称的子集执行此操作。看着重命名,但无法让它工作。 另外,当我转置时,我得到一个矩阵,这是为什么呢?

感谢帮助 亨里克

【问题讨论】:

意识到我可以在 dplyr 和命名字符中使用重命名。 df3 % rename(., !!df3) 但是如何导入命名字符列表? 【参考方案1】:

我们可以使用match

names(df)[-1] <- as.character(df2$Tumor_name[match(names(df)[-1], df2$Sample_name)])
df

#  Gene_Symbol   Tumor56   Tumor17   Tumor99    Tumor2  Tumor34  Tumor84  Tumor51
#1       Gene1  85657.98  10423.41  18778.68  23919.53       NA       NA       NA
#2       Gene2  54417.79  41660.73  43655.80  47829.02 46677.20 68596.23 80506.15
#3       Gene3 110949.33  40094.55        NA        NA 63389.45 56802.61 48722.99
#4       Gene4  53197.45  49519.78  57447.09  51478.58 48722.15 44712.64 38629.01
#5       Gene5  87156.80 129387.11 113266.15 116275.34       NA       NA       NA
#6       Gene6        NA        NA  44810.26  43110.95 77135.52 47744.18 37885.00
#7       Gene7  23880.28  23903.26  26316.65  25417.46 40265.69 33689.63 36638.02

【讨论】:

【参考方案2】:

我们可以从宽到长重新整形,合并,然后再重新整形到从长到宽:

library(dplyr)
library(tidyr)

pivot_longer(df, cols = starts_with("S"), names_to = "Sample_name") %>% 
  left_join(df2, by = "Sample_name") %>% 
  pivot_wider(id_cols = Gene_Symbol, names_from = Tumor_name, values_from = value)

## A tibble: 7 x 8
#  Gene_Symbol Tumor56 Tumor17 Tumor99  Tumor2 Tumor34 Tumor84 Tumor51
#  <chr>         <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#1 Gene1        85658.  10423.  18779.  23920.     NA      NA      NA 
#2 Gene2        54418.  41661.  43656.  47829.  46677.  68596.  80506.
#3 Gene3       110949.  40095.     NA      NA   63389.  56803.  48723.
#4 Gene4        53197.  49520.  57447.  51479.  48722.  44713.  38629.
#5 Gene5        87157. 129387. 113266. 116275.     NA      NA      NA 
#6 Gene6           NA      NA   44810.  43111.  77136.  47744.  37885 
#7 Gene7        23880.  23903.  26317.  25417.  40266.  33690.  36638.

【讨论】:

【参考方案3】:

利用dplyrstringr 的一种方法可能是:

df %>%
 rename_at(vars(starts_with("Sample")), 
           ~ str_replace_all(., setNames(df2$Tumor_name, df2$Sample_name)))

  Gene_Symbol   Tumor56   Tumor17   Tumor99    Tumor2  Tumor34  Tumor84  Tumor51
1       Gene1  85657.98  10423.41  18778.68  23919.53       NA       NA       NA
2       Gene2  54417.79  41660.73  43655.80  47829.02 46677.20 68596.23 80506.15
3       Gene3 110949.33  40094.55        NA        NA 63389.45 56802.61 48722.99
4       Gene4  53197.45  49519.78  57447.09  51478.58 48722.15 44712.64 38629.01
5       Gene5  87156.80 129387.11 113266.15 116275.34       NA       NA       NA
6       Gene6        NA        NA  44810.26  43110.95 77135.52 47744.18 37885.00
7       Gene7  23880.28  23903.26  26316.65  25417.46 40265.69 33689.63 36638.02

【讨论】:

以上是关于根据 dplyr 与另一个数据框的匹配更改数据框中的列名的主要内容,如果未能解决你的问题,请参考以下文章

将数据框的一列与另一列匹配,拉入其他列,组合成大数据集

如何根据从其他框中选择的文本更改动态创建的组合框的显示项

使用 OR 使用 dplyr 过滤数据框的更好方法?

使用源代码管理更改文本框的内容

如何基于每个数据框中具有不同名称的两列将两个数据框与 dplyr 连接起来? [复制]

如何根据共同日期在单个数据框中对齐两个数据框的数据[重复]