gpt4 book ai didi

r - R中表格上方的动态标签

转载 作者:行者123 更新时间:2023-12-02 02:54:15 24 4
gpt4 key购买 nike

我创建了一个表格,其中包含一些生产数据、预测数据以及两者之间的差异。对于这些类别中的每一个,我都想同时展示 6 年。如果我突然改为 3 年而不是 6 年,我很难在表格上方为每个类别贴上标签。

我的代码:

proddata <- read.table("C:/...Production.csv",sep=";" , header=TRUE)  
forcdata <- read.table("C:...Forecast.csv", sep=";",header=TRUE)

wells<-unique(proddata[3]) mergedAll<-NULL

for (i in 1:length(wells[[1]])){

prod_w<-subset(proddata,Well ==wells[i,1])
forc_w<-subset(forcdata,Well ==wells[i,1])

t_prod<-t(prod_w[,c(1,2)]) t_forc<-t(forc_w[,c(1,2)])

delta<-t_prod[c(1,2),]
delta[2,]<-as.numeric(t_prod[2,])-as.numeric(t_forc[2,])

merged<- data.frame(t_prod[c(1,2),] ,t_forc[c(1,2),] ,delta)

colnames(merged) <- merged[1, ] merged <- merged[-1, ]

rownames(merged)<-wells[i,1]

if (!exists("mergedAll")){ mergedAll<-merged } else {
mergedAll<-rbind(mergedAll,merged) } }

library(gridExtra) grid.table(mergedAll)

这是我想要的: Dynamic labels above the table

最佳答案

您可以为此使用 tableHTML

示例数据

df <- structure(list(X2014 = c(10L, 101L, 111L), X2015 = c(20L, 202L, 
212L), X2016 = c(30L, 303L, 313L), X2017 = c(40L, 404L, 414L),
X2018 = c(50L, 505L, 515L), X2019 = c(50L, 506L, 516L), X2014.1 = c(12L,
112L, 122L), X2015.1 = c(18L, 118L, 128L), X2016.1 = c(41L,
141L, 132L), X2017.1 = c(60L, 160L, 151L), X2018.1 = c(55L,
155L, 170L), X2019.1 = c(-2L, -11L, -11L), X2014.2 = c(2L,
84L, 84L), X2015.2 = c(-11L, 162L, 181L), X2016.2 = c(-20L,
244L, 263L), X2017.2 = c(-5L, 350L, 345L), X2018.2 = c(52L,
517L, 527L), X2019.2 = c(10L, 28L, 38L)), .Names = c("X2014",
"X2015", "X2016", "X2017", "X2018", "X2019", "X2014.1", "X2015.1",
"X2016.1", "X2017.1", "X2018.1", "X2019.1", "X2014.2", "X2015.2",
"X2016.2", "X2017.2", "X2018.2", "X2019.2"), class = "data.frame", row.names = c("W1",
"W2", "W3"))

基本解决方案

像这样设置你的动态输入:

years 是您数据框中的年份(注意:如果您的数据结构允许重复的列名,您也可以使用 unique(names(df)) 以获得相同的结果)。 num_years 计算它们以便您可以更改输入。 num_colstableHTML 对象的列数。

years <- 2014:2019
num_years <- length(years)
num_cols <- 1 + num_years * 3

library(tableHTML)

这将创建一个 tableHTML,其中年份是标题,标签是标题上方的第二个标题。

df %>% 
tableHTML(headers = rep(years, 3),
second_headers = list(c(1, rep(num_years, 3)),
c("", "Production", "Forecast", "Difference")),
widths = rep(100, num_cols))

结果是这样的:

basic_solution

添加CSS来格式化表格:

您可以使用 add_css_* 函数系列来设置 tableHTML 的样式:

df %>% 
tableHTML(headers = rep(years, 3),
second_headers = list(c(1, rep(num_years, 3)),
c("", "Production", "Forecast", "Difference")),
widths = rep(100, num_cols)) %>%
add_css_header(css = list("background-color",
"gray"),
headers = 1:num_cols) %>%
add_css_second_header(css = list("background-color",
"gray"),
second_headers = 1:num_cols) %>%
add_css_row(css = list("background-color",
"darkgray"),
rows = 4) %>%
add_css_row(css = list("background-color",
"lightgray"),
rows = c(3, 5))

这里的结果是:

css_solution

关于r - R中表格上方的动态标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50331452/

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