r - S4 类列表到数据框

标签 r list dataframe s4

我是 S4 类(class)的新手,四处寻找解决方案但失败了:( 我有一个 S4 类列表,不确定它是否是嵌套列表,但我想将它更改为数据框。我的列表看起来像这个名为 z

head(z)
[[1]]
An object of class "AgNode"
Slot "center":
x: 1515, y: 2258

Slot "name":
[1] "hsa:4052"

Slot "txtLabel":
An object of class "AgTextLabel"
Slot "labelText":
[1] "hsa:4052"

Slot "labelLoc":
x: 0, y: 0

Slot "labelJust":
[1] "n"

Slot "labelWidth":
[1] 50

[[2]]
An object of class "AgNode"
Slot "center":
x: 1443, y: 2567

Slot "name":
[1] "hsa:1311"

Slot "txtLabel":
An object of class "AgTextLabel"

等等,我想从中心槽中提取 X 和 Y 值,从名称槽中提取名称。并将这三个放在一个数据框中。我该怎么做?

see<-do.call(cbind, lapply(z, stack))

我试过了,但是报错

Error in as.list.default(x) : 
no method for coercing this S4 class to a vector

谁能帮帮我?

最佳答案

如果你想要一个“单行”:

purrr::map_df(x, ~as.list(set_names(getPoints(getNodeCenter(.)), c("x", "y"))))`

但如果没有性能影响,我想把这些拼写出来,因为最终我们是为人类编程:

library(Rgraphviz)
library(purrr)

V <- letters[1:10]
M <- 1
g1 <- randomGraph(V, M, .2)
z <- agopen(g1,name="foo")
x <- AgNode(z)

map(x, getNodeCenter) %>%
  map(getPoints) %>%
  map(set_names, c("x", "y")) %>%
  map_df(as.list)
## # A tibble: 10 × 2
##        x     y
##    <int> <int>
## 1     73   348
## 2     73   243
## 3    145   348
## 4    217   348
## 5    289   348
## 6     27   137
## 7     82    32
## 8    361   348
## 9    433   348
## 10   505   348

这也是:

library(magrittr)

lapply(x, getNodeCenter) %>%
  lapply(getPoints) %>%
  lapply(set_names, c("x", "y")) %>%
  lapply(as.list) %>% 
  do.call(rbind.data.frame, .)

如果您不想使用 purrr 但愿意使用管道或者:

do.call(rbind.data.frame, lapply(x, function(y) {
  as.list(setNames(getPoints(getNodeCenter(y)), c("x", "y")))
}))

如果你不喜欢滚边,或者:

do.call(rbind.data.frame, lapply(x, function(y) {
   tmp <- getNodeCenter(y)
   tmp <- getPoints(tmp)
   tmp <- setNames(tmp, c("x", "y"))
   as.list(tmp)
}))

如果您希望它可读并且不使用 purrr 或管道。

关于r - S4 类列表到数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996026/

相关文章:

javascript - 将一个巨大的 json 文件分成单独的文件

r - 在 R 中使用 for 循环时将列表转换为向量

list - 如何从自动模式列表中删除项目(Emacs)

python - 使得当我从一个列表中删除一个元素时,它也会从另一个列表中删除

r - 在数据帧列表上使用 lapply

r - 在 Linux Mint 18 上安装 R 包时出错

滚动回归数据框

python - 将 Python 字符串格式化与列表一起使用

dataframe - 如何将一列 Float64 全部填充 NA 值添加到 Julia DataFrame?

python - Pandas 熔化函数使用列索引位置而不是列名称