重命名 r kable 中的值
Rename values in r kable
我们以鸢尾花数据集为例
dat <- iris[1:10, ]
dat[, 1:4] <- round(dat[ ,1:4])
如您所见,前四列包含数值,最后一列包含字符值。
现在,在创建 kable table 时,我想重命名这些值,以便第一列包含字符值(0 变为“零”,2 变为“二”,等等),第二到第四列保持不变(即数字),最后一列中的值以大写字母(“Setosa”)开头。是否可以使用 kableExtra 包来实现这一点?如果我不必重新编码数据框中的值,我会更愿意。
这是一个使用不同包的解决方案。我希望这就是您要找的。
首先使用不同的包操作数据然后创建 kable table
library(tidyverse)
library(kableExtra)
library(xfun) # numbers_to_words function
# your data
dat <- iris[1:10, ]
dat[, 1:4] <- round(dat[ ,1:4])
# create kable table
dat1 <- dat %>%
mutate(Sepal.Length = numbers_to_words(Sepal.Length)) %>%
mutate(Species = str_to_title(Species)) %>% # from stringr package in tidyverse
kbl() %>%
kable_styling()
dat1 # call table
我们以鸢尾花数据集为例
dat <- iris[1:10, ]
dat[, 1:4] <- round(dat[ ,1:4])
如您所见,前四列包含数值,最后一列包含字符值。
现在,在创建 kable table 时,我想重命名这些值,以便第一列包含字符值(0 变为“零”,2 变为“二”,等等),第二到第四列保持不变(即数字),最后一列中的值以大写字母(“Setosa”)开头。是否可以使用 kableExtra 包来实现这一点?如果我不必重新编码数据框中的值,我会更愿意。
这是一个使用不同包的解决方案。我希望这就是您要找的。 首先使用不同的包操作数据然后创建 kable table
library(tidyverse)
library(kableExtra)
library(xfun) # numbers_to_words function
# your data
dat <- iris[1:10, ]
dat[, 1:4] <- round(dat[ ,1:4])
# create kable table
dat1 <- dat %>%
mutate(Sepal.Length = numbers_to_words(Sepal.Length)) %>%
mutate(Species = str_to_title(Species)) %>% # from stringr package in tidyverse
kbl() %>%
kable_styling()
dat1 # call table