首页 > 解决方案 > 替换特定的列名和行名

问题描述

我想用特定字符替换第一列(例如空单元格,后跟 110、111、210 等)和第一行(例如,空单元格,后跟 110、111、210 等) - 对于比如111我想改成A,310改成B。A和B之间没有关系,它们表示字符。此外,当使用 时rename(),如果缺少一个列名,则不会进行替换(例如,1111 与 B,但由于缺少 1111,因此不会替换 111 和 310)。我怎样才能避免这种情况?

原始数据:

在此处输入图像描述

期望的输出:

在此处输入图像描述

样本数据:

structure(list(X1 = c(110, 111, 210, 310, 1000, 1100, 1110, 1120
), `110` = c(NA, 0, 0.003552398, 0, 0, 0, 9.54e-05, 0), `111` = c(0.008264463, 
0, 0, 0, 0, 0, 0, 0), `210` = c(0.008264463, 0, 0.687388988, 
0.135135135, 0, 0.019230769, 0.014408397, 0.01369863), `310` = c(0, 
0, 0.006216696, 0.405405405, 0, 0, 0.00028626, 0), `1000` = c(0, 
0, 0.000888099, 0, 0, 0, 0, 0)), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -8L), spec = structure(list(
    cols = list(X1 = structure(list(), class = c("collector_double", 
    "collector")), `110` = structure(list(), class = c("collector_double", 
    "collector")), `111` = structure(list(), class = c("collector_double", 
    "collector")), `210` = structure(list(), class = c("collector_double", 
    "collector")), `310` = structure(list(), class = c("collector_double", 
    "collector")), `1000` = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

标签: rdataframe

解决方案


你需要这个吗?

前 1-2 个步骤仅用于创建像您这样的数据框,因此不需要。

xx <- structure(list(X1 = c(110, 111, 210, 310, 1000, 1100, 1110, 1120
), `110` = c(NA, 0, 0.003552398, 0, 0, 0, 9.54e-05, 0), `111` = c(0.008264463, 
                                                                  0, 0, 0, 0, 0, 0, 0), `210` = c(0.008264463, 0, 0.687388988, 
                                                                                                  0.135135135, 0, 0.019230769, 0.014408397, 0.01369863), `310` = c(0, 
                                                                                                                                                                   0, 0.006216696, 0.405405405, 0, 0, 0.00028626, 0), `1000` = c(0, 
                                                                                                                                                                                                                                 0, 0.000888099, 0, 0, 0, 0, 0)), class = c("spec_tbl_df", "tbl_df", 
                                                                                                                                                                                                                                                                            "tbl", "data.frame"), row.names = c(NA, -8L), spec = structure(list(
                                                                                                                                                                                                                                                                              cols = list(X1 = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                           "collector")), `110` = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                              "collector")), `111` = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                                                 "collector")), `210` = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "collector")), `310` = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "collector")), `1000` = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 "collector")), skip = 1L), class = "col_spec"))

library(tibble) 
xx <- xx %>% column_to_rownames('X1')
#your array is something like this
xx
#>              110         111         210         310        1000
#> 110           NA 0.008264463 0.008264463 0.000000000 0.000000000
#> 111  0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
#> 210  0.003552398 0.000000000 0.687388988 0.006216696 0.000888099
#> 310  0.000000000 0.000000000 0.135135135 0.405405405 0.000000000
#> 1000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
#> 1100 0.000000000 0.000000000 0.019230769 0.000000000 0.000000000
#> 1110 0.000095400 0.000000000 0.014408397 0.000286260 0.000000000
#> 1120 0.000000000 0.000000000 0.013698630 0.000000000 0.000000000

library(tidyverse)
dimnames(xx) <- map(dimnames(xx), ~ case_when(.x == '111' ~ 'A',
                              .x == '310' ~ 'B',
                              TRUE ~ .x))
xx
#>              110           A         210           B        1000
#> 110           NA 0.008264463 0.008264463 0.000000000 0.000000000
#> A    0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
#> 210  0.003552398 0.000000000 0.687388988 0.006216696 0.000888099
#> B    0.000000000 0.000000000 0.135135135 0.405405405 0.000000000
#> 1000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
#> 1100 0.000000000 0.000000000 0.019230769 0.000000000 0.000000000
#> 1110 0.000095400 0.000000000 0.014408397 0.000286260 0.000000000
#> 1120 0.000000000 0.000000000 0.013698630 0.000000000 0.000000000

reprex 包于 2021-06-20 创建 (v2.0.0 )


推荐阅读