简体   繁体   English

重命名 r kable 中的值

[英]Rename values in r kable

Let's take the iris dataset as an example.我们以 iris 数据集为例。

dat <- iris[1:10, ]
dat[, 1:4] <- round(dat[ ,1:4])

As you can see, the first four columns contain numerical values and the final column contains character values.如您所见,前四列包含数值,最后一列包含字符值。

Now, when creating a kable table, I would like to rename the values so that the first column contains character values (0 becomes "Zero", 2 becomes "Two", etc.), the second to forth columns remain the same (ie numerical) and the values in the final column starts with a capital letter ("Setosa").现在,在创建 kable 表时,我想重命名这些值,以便第一列包含字符值(0 变为“零”,2 变为“二”,等等),第二到第四列保持不变(即数字),最后一列中的值以大写字母(“Setosa”)开头。 Is it possible to achieve this using the kableExtra package?是否可以使用 kableExtra package 来实现这一点? I would prefer if I didn't have to recode the values inside the data frame.如果我不必重新编码数据框中的值,我会更喜欢。

Here is a solution with different packages.这是具有不同软件包的解决方案。 I hope this is what you are looking for.我希望这就是你要找的。 First manipulate the data with different packages then create kable table首先用不同的包操作数据然后创建kable表

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM