【问题标题】:Convert console output of list to a real R list将列表的控制台输出转换为真实的 R 列表
【发布时间】:2014-11-30 19:13:58
【问题描述】:

有人刚刚发布了一些控制台输出作为示例。 (这种情况经常发生,我有将打印输出转换为向量和数据帧的策略。)我想知道是否有人有一种优雅的方法可以将其解析为真正的 R 列表?

test <- "[[1]]
[1] 1.0000 1.9643 4.5957

[[2]]
[1] 1.0000 2.2753 3.8589

[[3]]
[1] 1.0000 2.9781 4.5651

[[4]]
[1] 1.0000 2.9320 3.5519

[[5]]
[1] 1.0000 3.5772 2.8560

[[6]]
[1] 1.0000 4.0150 3.1937

[[7]]
[1] 1.0000 3.3814 3.4291"

这是一个包含命名和未命名节点的示例:

 L <- 
structure(list(a = structure(list(d = 1:2, j = 5:6, o = structure(list(
    w = 2, 4), .Names = c("w", ""))), .Names = c("d", "j", "o"
)), b = "c", c = 3:4), .Names = c("a", "b", "c"))

> L
$a
$a$d
[1] 1 2

$a$j
[1] 5 6

$a$o
$a$o$w
[1] 2

$a$o[[2]]
[1] 4



$b
[1] "c"

$c
[1] 3 4

我已经完成了str 如何处理列表的代码,但它本质上是在进行逆变换。我认为这需要在某种程度上按照这些思路进行结构化,其中将递归调用类似这种逻辑的东西,因为可以命名列表(其中最后一个索引之前将有“$”)或未命名(在这种情况下“[[.]]”中会有一个数字。

parseTxt <- function(Lobj) {
   #setup logic
#  Untested code... basically a structure to be filled in
 rdLn <- function(Ln) {
     for( ln in length(inp) ) {
         m <- gregexpr("\\[\\[|\\$", "$a$o[[2]]")
         separators <- regmatches("$a$o[[2]]", m)
         curr.nm=NA
        if ( tail( separators, 1 ) == "$" ){ 
                   nm <- sub("^.+\\$","",ln)
                   if( !nm %in% curr.nm){ curr.nm <-c(nm, curr.nm) }
        } else { if (tail( separators, 1 ) == '[[' ){
            # here need to handle "[[n]]" case
        } else {  and here handle the "[n]" case
                    }
     }
 }

【问题讨论】:

  • 说真的,要求dput 输出。如果他们不提供,请投反对票并继续前进。你可以使用像lapply(readLines(textConnection(gsub("\n(?=\n)|\\[\\[\\d*\\]\\]\n|\\[\\d*\\]", "", test, perl=TRUE))), function(x) scan(textConnection(x)))这样的怪物,但我不会。
  • 我同意罗兰的观点。另一种怪物是read.delim(text=gsub("\\[+\\d+\\]+", "", test), header=FALSE, sep=""),但仅适用于这种情况。
  • @Andrie。甚至在这里都行不通。提供 3 列数据框,而不是 7 元素列表。
  • @BondedDust 真的。关闭,但没有雪茄。

标签: r list parsing


【解决方案1】:

这是我的解决方案。它适用于您的测试用例以及我测试过的其他几个测试用例。

deprint <- function(ll) {
    ## Pattern to match strings beginning with _at least_ one $x or [[x]]
    branchPat <- "^(\\$[^$[]*|\\[\\[[[:digit:]]*\\]\\])"
    ## Pattern to match strings with _just_ one $x or one [[x]]
    trunkPat <- "^(\\$[^$[]*|\\[\\[[[:digit:]]*\\]\\])\\s*$"
    ##
    isBranch <- function(X) {
        grepl(branchPat, X[1])
    }
    ## Parse character vectors of lines like "[1] 1 3 4" or
    ## "[1] TRUE FALSE" or c("[1] a b c d", "[5] e f") 
    readTip <- function(X) {
        X <- paste(sub("^\\s*\\[.*\\]", "", X), collapse=" ")
        tokens <- scan(textConnection(X), what=character(), quiet=TRUE)
        read.table(text = tokens, stringsAsFactors=FALSE)[[1]]
    }

    ## (0) Split into vector of lines (if needed) and
    ##     strip out empty lines
    ll <- readLines(textConnection(ll))
    ll <- ll[ll!=""]

    ## (1) Split into branches ...
    trunks <- grep(trunkPat, ll)
    grp <- cumsum(seq_along(ll) %in% trunks)
    XX <- split(ll, grp)
    ## ... preserving element names, where present
    nms <- sapply(XX, function(X) gsub("\\[.*|\\$", "", X[[1]]))
    XX <-  lapply(XX, function(X) X[-1])
    names(XX) <- nms

    ## (2) Strip away top-level list identifiers.
    ## pat2 <- "^\\$[^$\\[]*"
    XX <- lapply(XX, function(X) sub(branchPat, "", X))

    ## (3) Step through list elements:
    ## - Branches will need further recursive processing.
    ## - Tips are ready to parse into base type vectors.
    lapply(XX, function(X) {
        if(isBranch(X)) deprint(X) else readTip(X)
    })
}

使用L,您的更复杂的示例列表,它给出的内容如下:

## Because deprint() interprets numbers without a decimal part as integers,
## I've modified L slightly, changing "list(w=2,4)" to "list(w=2L,4L)" 
## to allow a meaningful test using identical(). 
L <-
structure(list(a = structure(list(d = 1:2, j = 5:6, o = structure(list(
    w = 2L, 4L), .Names = c("w", ""))), .Names = c("d", "j", "o"
)), b = "c", c = 3:4), .Names = c("a", "b", "c"))

## Capture the print representation of L, and then feed it to deprint()
test2 <- capture.output(L)
LL <- deprint(test2)
identical(L, LL)
## [1] TRUE
LL
## $a
## $a$d
## [1] 1 2
## 
## $a$j
## [1] 5 6
## 
## $a$o
## $a$o$w
## [1] 2
## 
## $a$o[[2]]
## [1] 4
## 
## $b
## [1] "c"
## 
## $c
## [1] 3 4

以下是它处理test 的打印表示的方式,这是您更常规的列表:

deprint(test)
## [[1]]
## [1] 1.0000 1.9643 4.5957
## 
## [[2]]
## [1] 1.0000 2.2753 3.8589
## 
## [[3]]
## [1] 1.0000 2.9781 4.5651
## 
## [[4]]
## [1] 1.0000 2.9320 3.5519
## 
## [[5]]
## [1] 1.0000 3.5772 2.8560
## 
## [[6]]
## [1] 1.0000 4.0150 3.1937
## 
## [[7]]
## [1] 1.0000 3.3814 3.4291

再举一个例子:

head(as.data.frame(deprint(capture.output(as.list(mtcars)))))
#    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

【讨论】:

  • 也适合我。谢谢。
  • 我昨天打算授予赏金并认为我是通过勾选答案来这样做的,但今天收到一条消息警告我它即将到期所以回来并单击蓝色 +500 图标
  • @BondedDust -- 谢谢。那是相当慷慨的赏金。我将不得不留意其他一些问题,以便分发它,从而传播节日的欢乐!
  • 我不认为 R 特别适合递归,尽管使用列表作为基本结构。当我开始学习 LisP 时,我永远无法适应它。我不擅长的另一个领域是迭代定义数据的顺序处理:Y_i &lt;- Y_(i-1)*a_i + e_i 我可以在 for 循环中执行此操作,但一直认为它应该可以重新定义为向量过程。
【解决方案2】:

我不会称其为“优雅”,但对于未命名的列表,您可以按照以下方式对某些内容进行检查/修改:

s <- strsplit(gsub("\\[+\\d+\\]+", "", test), "\n+")[[1]][-1]
lapply(s, function(x) scan(text = x, what = double(), quiet = TRUE))

[[1]]
[1] 1.0000 1.9643 4.5957

[[2]]
[1] 1.0000 2.2753 3.8589

[[3]]
[1] 1.0000 2.9781 4.5651

[[4]]
[1] 1.0000 2.9320 3.5519

[[5]]
[1] 1.0000 3.5772 2.8560

[[6]]
[1] 1.0000 4.0150 3.1937

[[7]]
[1] 1.0000 3.3814 3.4291

当然,这仅针对列表,并且此特定示例专门针对what = double(),因此需要进行额外检查。我突然想到检测列表中的字符元素的一个想法是制作what 参数

what = if(length(grep("\"", x))) character() else double()

【讨论】:

  • 您可以测试向量线是否存在" 以确定您运行的是哪种scan
猜你喜欢
  • 2023-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
相关资源
最近更新 更多