如何将不同的表在彼此上方的同一选项卡上保存到一个CSV中?

发布于 2025-01-19 10:09:38 字数 251 浏览 4 评论 0原文

假设我有 4 个表,我想将它们放入 csv 文件中,如下所示:

在此处输入图像描述

我该怎么做?我想要两列表和任意数量的行,并且我希望能够定义哪个表在哪里,并在两列之间保留 1 列,在每行表之间保留两行,如图所示。

Let's say I have 4 tables, and I want to put them in a csv file like this:

enter image description here

How would I do that? I want two columns of tables and any number of rows, and I want to be able to define where which table is and keep 1 column between the two columns and two rows between each row of tables as depicted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2025-01-26 10:09:39

将您的4个数据帧放入命名列表L,例如基于内置BOD数据框架所示的测试数据。

与问题一样,我们假设前两个数据帧中列数的总和等于最后两个数据帧中列数的总和。

定义fmt函数,该功能输入列表l的名称,并输出一个字符TS系列,上面名称为顶部。我们使用TS系列,因为这些系列允许cbind与具有不同数量的行的参数一起使用。最后,使用write.table输出结果。我们只是将其输出到控制台,但您可以将write.table.table语句适当地转换为文件。

没有包装。

# test data
b <- BOD; rownames(b) <- letters[1:nrow(BOD)]
L <- list(d1 = b[1:2, ], d2 = b[1:3, ], d3 = b[1:4, ], d4 = b[1:5,])

fmt <-  function(nm, DF = L[[nm]]) {
  m <- cbind(rownames(DF), sapply(DF, as.character))
  as.ts(rbind(c(nm, rep("", ncol(DF))), colnames(m), m))
}

m1 <- cbind(fmt("d1"), "", fmt("d2"))
m2 <- cbind(fmt("d3"), "", fmt("d4"))
m <- rbind(m1, "", m2)
write.table(m, sep = ",", na = "", 
  row.names = FALSE, col.names = FALSE, quote = FALSE)

给予:

d1,,,,d2,,
,Time,demand,,,Time,demand
a,1,8.3,,a,1,8.3
b,2,10.3,,b,2,10.3
,,,,c,3,19
,,,,,,
d3,,,,d4,,
,Time,demand,,,Time,demand
a,1,8.3,,a,1,8.3
b,2,10.3,,b,2,10.3
c,3,19,,c,3,19
d,4,16,,d,4,16
,,,,e,5,15.6

Put your 4 data frames into a named list L such as the test data shown below based on the built-in BOD data frame.

As in the question, we assume that the sum of the number of columns in the first two data frames equals the sum of the number of columns in the last two data frames.

Define a fmt function which inputs a name from the list L and outputs a character ts series with the name at top. We use ts series because those allow cbind to be used with arguments having different numbers of rows. Finally use write.table to output the result. We just output it to the console but you can modify the write.table statement appropriately to output to a file.

No packages are used.

# test data
b <- BOD; rownames(b) <- letters[1:nrow(BOD)]
L <- list(d1 = b[1:2, ], d2 = b[1:3, ], d3 = b[1:4, ], d4 = b[1:5,])

fmt <-  function(nm, DF = L[[nm]]) {
  m <- cbind(rownames(DF), sapply(DF, as.character))
  as.ts(rbind(c(nm, rep("", ncol(DF))), colnames(m), m))
}

m1 <- cbind(fmt("d1"), "", fmt("d2"))
m2 <- cbind(fmt("d3"), "", fmt("d4"))
m <- rbind(m1, "", m2)
write.table(m, sep = ",", na = "", 
  row.names = FALSE, col.names = FALSE, quote = FALSE)

giving:

d1,,,,d2,,
,Time,demand,,,Time,demand
a,1,8.3,,a,1,8.3
b,2,10.3,,b,2,10.3
,,,,c,3,19
,,,,,,
d3,,,,d4,,
,Time,demand,,,Time,demand
a,1,8.3,,a,1,8.3
b,2,10.3,,b,2,10.3
c,3,19,,c,3,19
d,4,16,,d,4,16
,,,,e,5,15.6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文