r - 替换特定的列名和行名
问题描述
我想用特定字符替换第一列(例如空单元格,后跟 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"))
解决方案
你需要这个吗?
前 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 )
推荐阅读
- javascript - 声明一个函数来等待一个承诺
- javascript - 如何从运行 Koa 的 Node 向客户端发送压缩字符串?
- mysql - 如何在 MySQL 中的另一列中共享相同值的表中返回重复项?
- r - 从不均匀的列表创建数据框
- sas - 在 SAS 中,如何转置一个表,为列中的每个唯一值生成一个虚拟变量
- python - 如何重新导入jupyter html文件?
- jquery - jQueryUI Combobox 不呈现“选择一个...”选项与空值
- amazon-web-services - 如何从 Hive 更新 DynamoDB 表
- arrays - 过滤数组元素以添加到 listView
- mysql - 为什么 MySQL Workbench 执行查询比我的应用程序慢得多?