gpt4 book ai didi

r - R 中的下划线列名

转载 作者:行者123 更新时间:2023-12-03 07:32:29 25 4
gpt4 key购买 nike

如何在 R 中为列名称添加下划线?

我尝试保存一个字符串,然后将其与

一起使用
library(crayon)

string1 <- underline("hello")
string2 <- underline("hello2")

colnames(table) <- c(string1, string2)

但是 string1 打印为 "\033[4mhello\033[24m"

String2 打印为 "\033[4mhello2\033[24m"

请告诉我如何使列名称带有下划线。

我只是想让列名脱颖而出,甚至在打印到控制台时更改文本的颜色也可以

最佳答案

矩阵和 data.frames 的默认打印代码在内部处理不可打印的字符,并对它们进行转义。这就是为什么 ANSI 转义字符代码'\033'被转义为''\033',而不是直接打印。

如果您不想这样做,则必须编写自己的 print.data.frame 函数,similar to how tibble does this 。正确地做到这一点需要相当多的逻辑(因此也需要代码)。不过,你可以作弊:

print.data.frame = function (x, ...) {
output = capture.output(base::print.data.frame(x, ...))
colnames = crayon::underline(colnames(x))
regmatches(output[1L], gregexpr('\\S+', output[1L]))[[1L]] = colnames
cat(output, sep = '\n')
}

捕获标准print.data.frame输出,并用下划线格式的版本替换第一行(=列标题)。

(请注意,如果列名称中存在空格,则上述代码将失败。)

关于r - R 中的下划线列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60546084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com