r无法读取dput输出

6yoyoihd  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(148)

代码和相关的dput输出生成一个有两行的表头。第一行是原来的列名。第二行是新列名。我想只显示第二行。我如何关闭/隐藏第一行?

library(flextable)
library(stringr)
d <- structure(list(ISO3 = c("AFG", "AGO", "ALB", "AND", "ARE", "ARG", "ARM", "AUS", "AUT", "AZE"), Name = c("Afghanistan", "Angola", "Albania", "Andorra", "United Arab Emirates", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan"), `annual_historical_1991-2010` = c(96, 84, 96, 98, 77, 93, 97, 88, 98, 94), `annual_ssp126_2041-2060` = c(94, 80, 94, 97, 71, 92, 96, 85, 97, 92), `annual_ssp126_2081-2100` = c(94, 80, 94, 97, 72, 92, 96, 85, 97, 91), `annual_ssp585_2041-2060` = c(93, 78, 93, 96, 69, 91, 95, 83, 97, 91), `annual_ssp585_2081-2100` = c(89, 69, 88, 93, 57, 87, 91, 77, 94, 85), `hot90_historical_1991-2010` = c(89, 76, 88, 94, 58, 87, 91, 76, 95, 83), `hot90_ssp126_2041-2060` = c(85, 71, 82, 90, 50, 84, 86, 72, 92, 77), `hot90_ssp126_2081-2100` = c(85, 71, 82, 90, 50, 84, 86, 72, 92, 76), `hot90_ssp585_2041-2060` = c(83, 68, 79, 88, 46, 82, 84, 70, 90, 74), `hot90_ssp585_2081-2100` = c(73, 56, 67, 79, 31, 75, 73, 59, 83, 60), `season_historical_1991-2010` = c(96, 84, 95, 98, 76, 93, 97, 92, 98, 93), `season_ssp126_2041-2060` = c(94, 81, 92, 96, 71, 91, 95, 90, 97, 91), `season_ssp126_2081-2100` = c(94, 80, 92, 96, 72, 91, 95, 90, 97, 90), `season_ssp585_2041-2060` = c(93, 78, 91, 95, 69, 90, 94, 89, 96, 89), `season_ssp585_2081-2100` = c(88, 69, 85, 91, 58, 86, 90, 84, 93, 84)), row.names = c(2L, 3L, 5L, 6L, 7L, 8L, 9L, 14L, 15L, 16L), class = "data.frame")

col_labels <- str_replace(names(d), "_ssp126_", " SSP1-2.6, ") |> 
  str_replace("_ssp585_", " SP5-8.5, ") |>
  str_replace("_historical_", " Historical, ") |>
  str_replace("annual ", "") |> 
  str_replace("hot90 ", "") |> 
  str_replace("season ", "")

names_list <-  as.list(setNames(col_labels, names(d)))

t_flex <- as_flextable(d)  |>
  set_header_labels(values = names_list)
t_flex
eit6fx6z

eit6fx6z1#

来自文件(?as_flextable.data.frame):
它显示前几行并显示列类型。
同样,set_header_labels
为弹性表底部行标题中的指定列设置标签。
因此,问题是as_flextable将给予您一个具有两行标题的表,其中第二行或最下面一行包含数据类型,然后通过set_header_labels将其替换为标签。
您可以使用flextable()

library(flextable)

flextable(d) |>
  set_header_labels(values = names_list)

相关问题