如何将列表导出到 R 中的自格式化文本文件中? [英] How to export list into self-formatted text file in R?

查看:28
本文介绍了如何将列表导出到 R 中的自格式化文本文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的列表:

I have a list which looks like this:

name <- c("A","B","C","D","E")
n <- c("1, 2, 3, 4, 5, 6, 7, 8, 9, 10", "11, 12, 13, 14, 15", "16, 17, 18, 19, 20", "21, 22, 23, 24, 25", "26, 27, 28, 29, 30, 31, 32, 33, 34, 35")
type <- c("a", "b", "c", "d", "e")

list <- list(name=name,n=n,type=type)

$name
[1] "A" "B" "C" "D" "E"

$n
[1] "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"          "11, 12, 13, 14, 15"                     "16, 17, 18, 19, 20"                    
[4] "21, 22, 23, 24, 25"                     "26, 27, 28, 29, 30, 31, 32, 33, 34, 35"

$type
[1] "a" "b" "c" "d" "e"

我需要将列表导出到一个文本文件中,如下所示:

I need to export the list in a text file looks like this:

name = "A",
type = "a",
n = 1, 2, 3, 4, 5,
    6, 7, 8, 9, 10
name = "B",
type = "b",
n = 11, 12, 13, 14, 15
name = "C",
type = "c",
n = 16, 17, 18, 19, 20
name = "D",
type = "d",
n = 21, 22, 23, 24, 25
name = "E",
type = "e",
n = 26, 27, 28, 29, 30,
    31, 32, 33, 34, 35

此文本文件遵循以下格式:

This text file follows the following format:

  • namentype 是一个组.这意味着,即 name=A", n=1,2,...10, type=a" 是一组记录.
  • 每个元素名称(namentype)位于每行的开头.
  • 每个元素内容(即11,12,13,14,15n的内容)放在元素名称.一些元素内容,在这种情况下,nametype中保存的数据被双引号"包围,但是n 中的那些没有被双引号包围.可以根据需要更改此设置.
  • 每个元素内容在每个元素内容的末尾用逗号,分隔,例如name = "A";,.
  • 需要注意的是,n元素内容已经包含了逗号,因为它是字符串类型的数据.但是,如果 n 包含 5 个以上的数字,则列应从新行开始.(这个数据好像有点棘手,最好先把string转成integer,因为每个数的长度不统一.)
  • 另一方面,最后一个元素内容,在这个例子中意味着type,它的末尾没有逗号作为一组新的namentype 从下一行开始
  • name, n and type are a group. It means that, i.e., name="A", n=1,2,...10, type="a" are one set of record.
  • Each element name (name, n and type) is placed at the beginning of each row.
  • Each element contents (i.e., 11, 12, 13, 14, 15 is a content of n) is placed after element name. Some element contents, in this case, data kept in name and type are surrounded by double quotation " but those in n are not surrounded by double quotation. This setting can be changed according to the needs.
  • Each element contents is separated by comma , at the end of each element contents, such as name = "A",.
  • It should be noted that element contents of n already contains comma as it is string type data. However, if n contains more than 5 numbers, column shall begin on the new line. (This data seems to be tricky. It may be better to convert string to integer first as the length of each number is not unified.)
  • On the other hand, the last element contents, which means type in this example, does not have a comma at its end as a new set of name, n and type starts from the next row

我曾经使用 R 进行更简单的数据操作,但从未将其用于此类文本操作.非常感谢您的建议.

I used to use R for more simple data manipulation but have never used it for this kind of text manipulation. Your suggestions are highly appreciated.

我更喜欢在 dplyr 中使用管道函数,但解决方案不限于此包.预先感谢您的友好建议.

I prefer to use pipe function in dplyr but the solution is not limited to this package. Thank you in advance for your kind suggestions.

推荐答案

对于 R 来说,这不是一个非常简单的字符串操作.字符串连接的核心是 paste()paste0().您可以使用 mapply() 同时迭代列表中的元素.基本上你只需要让所有的东西都对齐.例如

This isn't a very straightforward string manipulation for R. The heart of string concatenation is paste() and paste0(). You can use mapply() to iterate over the elements in your list at the same time. Basically you just have to get everything to line up. For example

rows <- paste0(mapply(function(name,n,type) {
  n <- strsplit(n, ", ")[[1]]
  parts <- unlist(tapply(n, (seq_along(n)-1)%/%5, paste, collapse=", "))
  val <- paste0(paste(
    paste0("name = ", dQuote(name, q=FALSE)),
    paste0("type = ", dQuote(type, q=FALSE)),
    paste0(rep(c("n = ", "    "), c(1, length(parts)-1)), parts, collapse=",\n"),
    sep = ",\n"), "\n")
}, 
    list$name, list$n, list$type, SIMPLIFY=TRUE), collapse="")

cat(rows)

返回

name = "A",
type = "a",
n = 1, 2, 3, 4, 5,
    6, 7, 8, 9, 10
name = "B",
type = "b",
n = 11, 12, 13, 14, 15
name = "C",
type = "c",
n = 16, 17, 18, 19, 20
name = "D",
type = "d",
n = 21, 22, 23, 24, 25
name = "E",
type = "e",
n = 26, 27, 28, 29, 30,
    31, 32, 33, 34, 35

一定要使用 cat() 来在输出中扩展换行符.

Be sure to use cat() to the newline characters are expanded in the output.

这篇关于如何将列表导出到 R 中的自格式化文本文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆