如何在表中按列设置数字格式(tableGrob)

如何在表中按列设置数字格式(tableGrob),r,R,我一直在尝试学习如何在R中设置输出表格的格式。现在我正在尝试将表格导入并设置列中数字的格式。我希望在某些列中有两个数字,而在某些列(如秩列)中没有。此外,我希望将行名称保留为年-月。如何创建这样的表 注:“萼片带”一栏中的“3”也打印为“3”,而不是“3.0” 库(gtable) 图书馆(网格) 图书馆(gridExtra) 图书馆(动物园) iris像这样的东西可能会帮你找到你想要的格式 library(gtable) library(grid) library(gridExtra) libr

我一直在尝试学习如何在R中设置输出表格的格式。现在我正在尝试将表格导入并设置列中数字的格式。我希望在某些列中有两个数字,而在某些列(如秩列)中没有。此外,我希望将行名称保留为年-月。如何创建这样的表

注:“萼片带”一栏中的“3”也打印为“3”,而不是“3.0”

库(gtable)
图书馆(网格)
图书馆(gridExtra)
图书馆(动物园)

iris像这样的东西可能会帮你找到你想要的格式

library(gtable)
library(grid)
library(gridExtra)
library(zoo)

iris <- as.matrix(iris[1:4, 1:3])
rownames(iris)<-as.yearmon(seq(as.Date("2000/1/1"), as.Date("2000/4/1"), by = "month"))
RankColumn<-seq(1, 4, by = 1)
iris<-cbind(iris, RankColumn)

# Create the matrix
iris<- as.matrix(iris)


# a simple function to scale each column to the range [0, 1]
norm <- function(x) {
  apply(x, 2, function(y){(y-min(y))/(max(y)-min(y))})
}

# function to format columns
format.column <- function(matrix, colnum, rounding, decimals){
  formatted <- format(round(as.numeric(matrix[,colnum]), digits = rounding),nsmall = decimals)
  return(formatted)
}

bluecol <- colorRamp(c("#3366EE", "#AABBFF", "#DDDDFF"))(norm(iris))
bluecol <- rgb(bluecol[, 1], bluecol[, 2], bluecol[, 3], max=255)

tt <- ttheme_default(core=list(bg_params=list(fill=bluecol)))

# Set formatting for individual columns 
# Adjust these or add additional columns to format as necessary
iris[,1] <- format.column(matrix = iris, colnum = 1, rounding = 2, decimals = 2)
iris[,2] <- format.column(matrix = iris, colnum = 2, rounding = 2, decimals = 1)

g <- tableGrob(iris, theme=tt)
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 1, l = 1, r = ncol(g))
grid.draw(g)
库(gtable)
图书馆(网格)
图书馆(gridExtra)
图书馆(动物园)

iris像这样的东西可能会帮你找到你想要的格式

library(gtable)
library(grid)
library(gridExtra)
library(zoo)

iris <- as.matrix(iris[1:4, 1:3])
rownames(iris)<-as.yearmon(seq(as.Date("2000/1/1"), as.Date("2000/4/1"), by = "month"))
RankColumn<-seq(1, 4, by = 1)
iris<-cbind(iris, RankColumn)

# Create the matrix
iris<- as.matrix(iris)


# a simple function to scale each column to the range [0, 1]
norm <- function(x) {
  apply(x, 2, function(y){(y-min(y))/(max(y)-min(y))})
}

# function to format columns
format.column <- function(matrix, colnum, rounding, decimals){
  formatted <- format(round(as.numeric(matrix[,colnum]), digits = rounding),nsmall = decimals)
  return(formatted)
}

bluecol <- colorRamp(c("#3366EE", "#AABBFF", "#DDDDFF"))(norm(iris))
bluecol <- rgb(bluecol[, 1], bluecol[, 2], bluecol[, 3], max=255)

tt <- ttheme_default(core=list(bg_params=list(fill=bluecol)))

# Set formatting for individual columns 
# Adjust these or add additional columns to format as necessary
iris[,1] <- format.column(matrix = iris, colnum = 1, rounding = 2, decimals = 2)
iris[,2] <- format.column(matrix = iris, colnum = 2, rounding = 2, decimals = 1)

g <- tableGrob(iris, theme=tt)
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 1, l = 1, r = ncol(g))
grid.draw(g)
库(gtable)
图书馆(网格)
图书馆(gridExtra)
图书馆(动物园)

iris虽然功能强大且灵活,但我发现Matt的
格式
功能有点过于复杂。一点
sprintf
就足够了。此外,将
yearmon
对象转换为字符将保留格式

library(gtable)
library(grid)
library(gridExtra)
library(zoo)

data(iris)
iris <- iris[1:4, 1:3]
rownames(iris) <- as.character(as.yearmon(
  seq(as.Date("2000/1/1"), as.Date("2000/4/1"), by = "month")))
iris$RankColumn <- 1:nrow(iris)

# a simple function to scale each row or column to the range [0, 1]
# will convert characters to numerics if in a sensible format
norm <- function(x, mar=2) {
    rnames <- rownames(x)
    x <- apply(x, 2, as.numeric)
    x <- apply(x, mar, function(y){(y-min(y))/(max(y)-min(y))})
    rownames(x) <- rnames
    x
}

# function to pad with zero
# by default does not pad integers
zeropad <- function(x, nz=1, exc.int=TRUE) {
    if (is.integer(x) & exc.int) {
        x
    } else { 
        sprintf(paste0("%.", nz, "f"), x)
    }
}

bluecol <- colorRamp(c("#3366EE", "#AABBFF", "#DDDDFF"))(norm(iris))
bluecol <- rgb(bluecol[, 1], bluecol[, 2], bluecol[, 3], max=255)

tt <- ttheme_default(core=list(bg_params=list(fill=bluecol)))

# convert floats to zero-padded characters
iris[1:ncol(iris)] <- sapply(iris, zeropad, 2)

g <- tableGrob(iris, theme=tt)
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 1, l = 1, r = ncol(g))
plot.new()
grid.draw(g)
库(gtable)
图书馆(网格)
图书馆(gridExtra)
图书馆(动物园)
数据(iris)

iris虽然功能强大且灵活,但我发现Matt的
格式
功能有点过于复杂。一点
sprintf
就足够了。此外,将
yearmon
对象转换为字符将保留格式

library(gtable)
library(grid)
library(gridExtra)
library(zoo)

data(iris)
iris <- iris[1:4, 1:3]
rownames(iris) <- as.character(as.yearmon(
  seq(as.Date("2000/1/1"), as.Date("2000/4/1"), by = "month")))
iris$RankColumn <- 1:nrow(iris)

# a simple function to scale each row or column to the range [0, 1]
# will convert characters to numerics if in a sensible format
norm <- function(x, mar=2) {
    rnames <- rownames(x)
    x <- apply(x, 2, as.numeric)
    x <- apply(x, mar, function(y){(y-min(y))/(max(y)-min(y))})
    rownames(x) <- rnames
    x
}

# function to pad with zero
# by default does not pad integers
zeropad <- function(x, nz=1, exc.int=TRUE) {
    if (is.integer(x) & exc.int) {
        x
    } else { 
        sprintf(paste0("%.", nz, "f"), x)
    }
}

bluecol <- colorRamp(c("#3366EE", "#AABBFF", "#DDDDFF"))(norm(iris))
bluecol <- rgb(bluecol[, 1], bluecol[, 2], bluecol[, 3], max=255)

tt <- ttheme_default(core=list(bg_params=list(fill=bluecol)))

# convert floats to zero-padded characters
iris[1:ncol(iris)] <- sapply(iris, zeropad, 2)

g <- tableGrob(iris, theme=tt)
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 1, l = 1, r = ncol(g))
plot.new()
grid.draw(g)
库(gtable)
图书馆(网格)
图书馆(gridExtra)
图书馆(动物园)
数据(iris)

iris我相信您必须将“apply(iris[,-4],2,zeropad)”一行更改为mapply(zeropad,iris[,-4],2)@user2946746:两者都可以工作。如果你想用两个零填充,你可以这样做:
apply(iris[,-4],2,zeropad,2)
mapply(zeropad,iris[,-4],2)
sapply(iris[,-4],zeropad,2)
。它们都会给出相同的结果。我倾向于在数组和矩阵上使用
apply
,在类似列表的对象(包括数据帧)上使用
*apply
(包括
mapply
sapply
)。谢谢,我尝试以不同的格式设置每一列。iris[,c(1)]如果在应用颜色函数之前对列进行格式化,它将无法正常工作,因为格式化会将数字转换为字符,并且无法对字符进行数学运算(至少不能直接进行)。如果字符的格式可以理解为数字,则可以对其进行转换。试试看:
ch我也想出了一个解决办法。iris[]我认为您必须将“apply(iris[,-4],2,zeropad)”一行更改为mapply(zeropad,iris[,-4],2)@user2946746:两者都可以工作。如果你想用两个零填充,你可以这样做:
apply(iris[,-4],2,zeropad,2)
mapply(zeropad,iris[,-4],2)
sapply(iris[,-4],zeropad,2)
。它们都会给出相同的结果。我倾向于在数组和矩阵上使用
apply
,在类似列表的对象(包括数据帧)上使用
*apply
(包括
mapply
sapply
)。谢谢,我尝试以不同的格式设置每一列。iris[,c(1)]如果在应用颜色函数之前对列进行格式化,它将无法正常工作,因为格式化会将数字转换为字符,并且无法对字符进行数学运算(至少不能直接进行)。如果字符的格式可以理解为数字,则可以对其进行转换。试试看:
ch我也想出了一个解决办法。iris[]我注意到图表丢失了颜色的格式。它们不再是按列而是按列。我注意到图表丢失了颜色的格式。它们不再是按列而是按列。