列出和取消列出行为不符合预期

时间:2019-02-19 18:20:15

标签: r dataframe zoo

为什么取消列表不将列表分解为最初放入列表的部分?

假设我有以下内容:

v <- zoo(data.frame(a=c(1,2,3), b=c("a","b","c")))

v,我得到:

> v
  a b
1 1 a
2 2 b
3 3 c

> str(v)
‘zoo’ series from 1 to 3
  Data: chr [1:3, 1:2] "1" "2" "3" "a" "b" "c"
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "a" "b"
  Index:  int [1:3] 1 2 3

(1)现在,我尝试将其填充到列表中:

l <- list(one=v, two=v)

然后我尝试取消列出,我本来希望有两个动物园对象。但是相反,我什么都没得到zoo类型的信息:

> unlist(l)
one1 one2 one3 one4 one5 one6 two1 two2 two3 two4 two5 two6 
 "1"  "2"  "3"  "a"  "b"  "c"  "1"  "2"  "3"  "a"  "b"  "c"

> str(unlist(l))
 Named chr [1:12] "1" "2" "3" "a" "b" "c" "1" "2" "3" "a" "b" "c"
 - attr(*, "names")= chr [1:12] "one1" "one2" "one3" "one4" ...

> typeof(unlist(l))
[1] "character"
> class(unlist(l))
[1] "character"

(2)为了理解它,我尝试将1个动物园对象放入列表中:

l <- list(one=v)

当我看着v时,期望获得与上述相同的输出,但令人惊讶的是:

> unlist(l)
one1 one2 one3 one4 one5 one6 
 "1"  "2"  "3"  "a"  "b"  "c" 

> str(unlist(l))
 Named chr [1:6] "1" "2" "3" "a" "b" "c"
 - attr(*, "names")= chr [1:6] "one1" "one2" "one3" "one4" ...

> typeof(unlist(l))
[1] "character"
> class(unlist(l))
[1] "character"

我可能没有看到R的一些细微之处,但我不明白我是如何无法从列表中找回原始的动物园对象的(尤其是在1元素列表的情况下)...

0 个答案:

没有答案