且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

拆分后的数据框列名称

更新时间:2023-11-18 21:38:52

The split 的输出是列表 ds [1] 返回列表,而 ds [[1]] 返回该第一个列表项中的值。

The output of split is a list. ds[1] returns a list, while ds[[1]] returns the value within that first list item.

示例:

ds[1]
# $bar.bar.bar
#     A   B   C D E
# 4 bar bar bar 4 8

ds[[1]]
#     A   B   C D E
# 4 bar bar bar 4 8

获取输出正确地编写为CSV文件,则需要提取实际的 data.frame ,因此需要使用 ds [[1]] 方法。

To get the output properly written as a CSV file, you need to extract the actual data.frame, so you need to use the ds[[1]] approach.

write.table(ds[[1]], file="foo.csv", append=FALSE, row.names=FALSE)

如果要编写所有 data.frame 来分隔CSV文件,您可以执行以下操作:

If you wanted to write all of the data.frames to separate CSV files, you can do something like:

lapply(names(ds), function(x) {
  write.table(ds[[x]], file = paste(x, ".csv", collapse = ""),
              append = FALSE, row.names = FALSE)
})

这将创建四个CS工作目录中的V文件(名为bar.bar.bar.csv,bar.foo.bla.csv,foo.foo.bla.csv和foo.bar.foo.csv)。

This will create four CSV files (named bar.bar.bar.csv, bar.foo.bla.csv, foo.foo.bla.csv, and foo.bar.foo.csv) in your working directory.