R语言 创建带有空单元格的表格(例如,不同长度的列)

dohp0rv5  于 2023-02-17  发布在  其他
关注(0)|答案(4)|浏览(153)

为了传达R分析的结果,我希望创建并导出一个空单元格的结果表(见下表)。
如果我尝试创建一个这种风格的 Dataframe 或tibble,它不会工作,因为事实上,列的大小不相等,因此返回错误。我不希望用NA填充空单元格,因为我不能发布带有NA的表格。有没有一种方法可以创建这样一个表格,它只用于报告结果,而不是R分析?
下面是我希望创建的结果表布局的虚拟示例。
我同时使用r和tidyverse。
| 数据集名称|x1|x2|三倍|四倍|x5|x6|x7|
| - ------|- ------|- ------|- ------|- ------|- ------|- ------|- ------|
| y1|1个|五个|1个|四个|四个|三个|十个|
| y2|四个|五个|第二章|三十四|十二|五个|五个|
| y3|无|无|六个|十个|1个|第二章|三十二|
| y4|1个|八个|第二章|四十五||||
| 第五年|第二章|九|1个|||||
| y6|六个|第二章|四个|||||
| y7|三个|四个|五个|||||
| y8|九十|二十个|六十|||||

Sample data 

x1 <- sample(1:40, 8)
x2 <- sample(1:40, 8)
x3 <- sample(1:40, 8)
x4 <- sample(1:40, 4)
x5 <- sample(1:40, 3)
x6 <- sample(1:40, 3)
x7 <- sample(1:40, 3)
qacovj5a

qacovj5a1#

Here's an example using gt from the data sample:

library(tidyverse)
library(gt)

list(
  x1 = sample(1:40, 8),
  x2 = sample(1:40, 8),
  x3 = sample(1:40, 8),
  x4 = sample(1:40, 4),
  x5 = sample(1:40, 3),
  x6 = sample(1:40, 3),
  x7 = sample(1:40, 3)
) |>
  map(`length<-`, 8) |>
  as_tibble() |> 
  mutate(dataset_name = paste0("y", 1:8)) |> 
  gt() |> 
  cols_move_to_start(dataset_name) |> 
  sub_missing(missing_text = "")
dataset_namex1x2x3x4x5x6x7
y120241015381223
y2264301943713
y3816163052824
y43821320
y561133
y6112924
y771327
y8172228

Using map(length<-, 8) does expand each to the full length by adding NA s, but sub_missing(missing_text = "") will then format the output as printing blank cells. You can output the gt result to a pdf/html/docx file directly or included in an rmarkdown document.

2ic8powd

2ic8powd2#

使用下面的示例数据,您可以将它们存储在列表中,使用lapply进行转换,然后通过do.call转换cbind,以生成所需的输出:
数据

dataset_name <- paste0("y", 1:8)
x1 <- x2 <- x3 <- 1:8
x4 <- 1:4
x5 <- x6 <- x7 <- 1:3

代码:

l1 <- list(dataset_name, x1, x2, x3, x4, x5, x6, x7)
l2 <- lapply(l1, `length<-`, max(lengths(l1)))
tble <- do.call(cbind, l2)
tble[is.na(tble)] <- "" # replace NAs with blanks, if desired

输出:

#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,] "y1" "1"  "1"  "1"  "1"  "1"  "1"  "1" 
#[2,] "y2" "2"  "2"  "2"  "2"  "2"  "2"  "2" 
#[3,] "y3" "3"  "3"  "3"  "3"  "3"  "3"  "3" 
#[4,] "y4" "4"  "4"  "4"  "4"  ""   ""   ""  
#[5,] "y5" "5"  "5"  "5"  ""   ""   ""   ""  
#[6,] "y6" "6"  "6"  "6"  ""   ""   ""   ""  
#[7,] "y7" "7"  "7"  "7"  ""   ""   ""   ""  
#[8,] "y8" "8"  "8"  "8"  ""   ""   ""   ""

如果这不是你的数据看起来像,请编辑您的问题,提供样本数据,我会更新。

rbl8hiat

rbl8hiat3#

一个简单的方法是将x变量转换为字符,并添加足够的空格,使它们都具有相同的长度。下面是一个函数,它可以完成此操作并生成一个 Dataframe :

x1 <- sample(1:40, 8)
x2 <- sample(1:40, 8)
x3 <- sample(1:40, 8)
x4 <- sample(1:40, 4)
x5 <- sample(1:40, 3)
x6 <- sample(1:40, 3)
x7 <- sample(1:40, 3)

ragged <- function(...) {
  args <- list(...)
  names <- as.character(substitute(list(...)))[-1]
  lens <- sapply(args, length)
  maxlen <- max(lens)
  args <- lapply(args, function(x) c(as.character(x), rep("", maxlen - length(x))))
  df <- do.call(data.frame, args)
  names(df) <- names
  df
}

ragged(x1, x2, x3, x4, x5, x6, x7)
#>   x1 x2 x3 x4 x5 x6 x7
#> 1 20 23 23 24 17 19 33
#> 2 10  7 38 13 38 22 26
#> 3  7 11 24 17 32 25  4
#> 4 34 24  1 33         
#> 5 25  5 31            
#> 6 19  9 18            
#> 7 32  6 35            
#> 8 28 28  5

创建于2023年2月14日,使用reprex v2.0.2

wvyml7n5

wvyml7n54#

使用cbind.na

out <- data.frame(dataset_name = paste0("y", 1:8), 
   do.call(qpcR:::cbind.na, mget(ls(pattern = '^x\\d+'))))
out[is.na(out)] <- ""
  • 输出
> out
  dataset_name x1 x2 x3 x4 x5 x6 x7
1           y1 32 19 20 35  8  8 14
2           y2  9  2 35  6 12 11 15
3           y3 31  9 16  5 25 39  9
4           y4 35 35 30 38         
5           y5 34 37 40            
6           y6  2 20 29            
7           y7 22  8 31            
8           y8 21 23 17

相关问题