S4类列表到数据框

时间:2016-10-12 10:12:13

标签: r list dataframe s4

我是S4类的新手,并四处寻找解决方案,但失败了:( 我有一个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

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

如果你想要“一线”:

# This file is auto-generated during the composer install
parameters:
    database_driver: pdo_mysql
    database_host: mysql
    database_port: 3306
    database_name: symfony
    database_user: root
    database_password: password

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

$ mv app/config/parameters.gitlab.yml app/config/parameters.yml.dist
$ ping -c 3 mysql
PING mysql (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: icmp_seq=0 ttl=64 time=0.282 ms
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.140 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.151 ms
--- mysql ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.140/0.191/0.282/0.065 ms
$ php -v
PHP 5.6.26 (cli) (built: Sep 23 2016 21:22:39) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
$ php composer.phar clear-cache
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Clearing cache (cache-dir): /root/.composer/cache
Clearing cache (cache-files-dir): /root/.composer/cache/files
Clearing cache (cache-repo-dir): /root/.composer/cache/repo
Cache directory does not exist (cache-vcs-dir): 
All caches cleared.
$ php composer.phar install
......
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache

  [Doctrine\DBAL\Exception\ConnectionException]                              
  An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused  

  [Doctrine\DBAL\Driver\PDOException]        
  SQLSTATE[HY000] [2002] Connection refused  

  [PDOException]                             
  SQLSTATE[HY000] [2002] Connection refused  

Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception

那也是:

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"))) })) 或管道。