Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R S4类列表到数据帧_R_List_Dataframe_S4 - Fatal编程技术网

R S4类列表到数据帧

R S4类列表到数据帧,r,list,dataframe,s4,R,List,Dataframe,S4,我参加了一个新的S4课程,四处寻找解决方案,但失败了:( 我有一个S4类列表,不确定它是否是嵌套列表,但我想将其更改为数据帧 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"

我参加了一个新的S4课程,四处寻找解决方案,但失败了:( 我有一个S4类列表,不确定它是否是嵌套列表,但我想将其更改为数据帧

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))
有人能帮我吗?

如果你想要“一行”:

但我想在性能没有受到影响的情况下解释一下,因为我们最终是为人类编程的:

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
如果您不想使用
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)
}))
do.call(rbind.data.frame,lappy(x,函数y){
tmp
library(magrittr)

lapply(x, getNodeCenter) %>%
  lapply(getPoints) %>%
  lapply(set_names, c("x", "y")) %>%
  lapply(as.list) %>% 
  do.call(rbind.data.frame, .)
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)
}))