0

我已经苦苦挣扎了两天,想找到一种从嵌套列表中创建特定矩阵的方法

首先,如果我没有正确解释我的问题,我很抱歉我是 StackOverflow* 和 R(以及编程......)的一周新手!

我使用您可以在那里找到的文件:

使用rjson,我有一个这样的 嵌套列表:MEP 投票的嵌套列表

List of 23905
 $ :List of 7
  ..$ ts     : chr "2004-12-16T11:49:02"
  ..$ url    : chr "http://www.europarl.europa.eu/RegData/seance_pleniere/proces_verbal/2004/12-16/votes_nominaux/xml/P6_PV(2004)12-16(RCV)_XC.xml"
  ..$ voteid : num 7829
  ..$ title  : chr "Projet de budget général 2005 modifié - bloc 3"
  ..$ votes  :List of 3
  .. ..$ +:List of 2
  .. .. ..$ total : num 45
  .. .. ..$ groups:List of 6
  .. .. .. ..$ ALDE   :List of 1
  .. .. .. .. ..$ : Named num 4404
  .. .. .. .. .. ..- attr(*, "names")= chr "mepid"
  .. .. .. ..$ GUE/NGL:List of 25
  .. .. .. .. ..$ : Named num 28469
  .. .. .. .. .. ..- attr(*, "names")= chr "mepid"
  .. .. .. .. ..$ : Named num 4298
  .. .. .. .. .. ..- attr(*, "names")= chr "mepid"

那么我的目标是有这样的东西: 最终矩阵

首先,我只想保留包含 $vote$+$groups$Renew 或 $vote$-$groups$Renew 或 $vote$'0'$groups 的列表(从 [[1]] 到 [[23905]]) $续订。主要名单(23905)是登记投票。我的工作是在 Renew 组上,所以我唯一的兴趣是在存在 Renew 组的地方进行投票,以便将它们与其他组进行比较。

之后,我的目标是在所有 [[x]] 中创建一个这样的矩阵,我们可以在其中找到 groups$Renewexists: final matrix

 V1             V2 (not mandatory)   V3[[x]]$voteid
[mepid==666] GUE/NGL     + (mepid==[666] is found in [[1]]$vote$+$groups$GUE/NGL)              
[mepid==777] Renew       - (mepid==[777] is found in [[1]]$vote$-$groups$GUE/NGL)             

我想创建一个矩阵,以便处理每个 MEP 的投票(由他们的 MEPid 引用)。他们的投票是+(赞成)、-(反对)或0(弃权)。此外,我希望 MEP 的政治团体显示在他们的 mepid 旁边的列中。由于存储选票的地方,我们可以找到他们的政治团体。如果 mepid 显示在列表 [[x]]$vote$+$groups$GUE/NGL 中,她或他属于 GUE/NGL 组。

我想做的可能看起来像这样

# Clean the nested list
Keep Vote[[x]] if Vote[[x]] list contain ,
$vote$+$groups$Renew, 
or $vote$-$groups$Renew,
or $vote$'0'$groups$Renew

# Create the matrix (or a data.frame if it is easier)
VoteMatrix <- as.matrix(
V1 = all "mepid" found in the nested list
V2 = groups (name of the list where we can find the mepid) (not mandatory)
V3 to Vy = If.else(mepid is in [[x]]$vote$+ then “+”,
mepid is in [[x]]$vote$- then “-“, "0")
)

先感谢您,

*尽管如此,自从我开始 R 以来,我一直在积极阅读这个网站!

4

1 回答 1

1

您可以看到'votes'子列表由三项组成,成员编号列表存储在我认为是党代号的地方。以下是您可以按政党“理顺”积极选民“memids”的方法:

str( unlist(  sapply(names(jlis[[1]]$votes$'+'$groups), function(x) unlist(jlis[[1]]$votes$'+'$groups[[x]]) ) ) )
 Named num [1:104] 28268 4514 28841 28314 28241 ...
 - attr(*, "names")= chr [1:104] "ALDE.mepid" "ALDE.mepid" "ALDE.mepid" "ALDE.mepid" ...

您将获得一个包含 108 个条目的命名数字向量。也许这将展示使用什么样的术语来更好地描述您想要的结果。(只为所需结果提供部分模式会留下太多歧义,无法支持完全形成的请求。)

我从您的链接下载的任何地方都没有看到数字 23905。我们显然在查看不同的数据。我看到这个时间戳:chr "2004-12-01T15:20:31"。我不会因为不了解 R 而让您懈怠,因为该任务需要用自然语言进行充分解释。如果英语不是您的母语,我会在语法方面放松您,但您肯定需要在解释方面做出更好的努力。这是我votes$'+'$groups在前三个项目的子列表中看到的名称,但由于 RENEW 不在其中任何一个项目中,因此在挑选项目方面没有太多可以证明的东西:

> names( jlis[[1]]$votes$'+'$groups)
[1] "ALDE"    "GUE/NGL" "IND/DEM" "NI"      "PPE-DE"  "PSE"     "UEN"    
> names( jlis[[2]]$votes$'+'$groups)
[1] "GUE/NGL" "IND/DEM" "NI"      "PPE-DE" 
> names( jlis[[3]]$votes$'+'$groups)
[1] "ALDE"      "GUE/NGL"   "IND/DEM"   "NI"        "PPE-DE"    "PSE"       "UEN"       "Verts/ALE"

此外,当我使用此方法查看所有可能的投票值时(对于您提供的所有三个项目),我仍然看不到任何RENEW名称。

sapply( jlis[[1]]$votes[c("+","-","0")], function(x) names(x$groups) )

第二次编辑后:这是隔离那些包含“更新”值的投票的下一步。我假设它可能仅在三个可能的“投票”值之一中具有“更新”值(+,-。 0). 如果没有(并且当它们中的任何一个中有一个时,它们中的每个总是有“更新”值),那么您也许可以简化逻辑。我们制作三个逻辑向量:

 sapply( seq_along(MEPVotes)  , function(i){ 'Renew' %in% names( MEPVotes[[i]]$votes[['0']][['groups']]) } )
#[1] FALSE FALSE FALSE  TRUE
 sapply( seq_along(MEPVotes)  , function(i){ 'Renew' %in% names( MEPVotes[[i]]$votes[['+']][['groups']]) } )
#[1] FALSE FALSE FALSE  TRUE
 sapply( seq_along(MEPVotes)  , function(i){ 'Renew' %in% names( MEPVotes[[i]]$votes[['-']][['groups']]) } )
#[1] FALSE FALSE FALSE  TRUE

然后将它们包装在matrix3 列的调用中并取每行的最大值(c(TRUE,FALSE) 的最大值为 1,然后转换回逻辑。

selection_vec = as.logical( apply( matrix( c( 
   sapply( seq_along(MEPVotes)  , function(i){ 'Renew' %in% names( MEPVotes[[i]]$votes[['0']][['groups']]) } ),
   sapply( seq_along(MEPVotes)  , function(i){ 'Renew' %in% names( MEPVotes[[i]]$votes[['+']][['groups']]) } ),
   sapply( seq_along(MEPVotes)  , function(i){ 'Renew' %in% names( MEPVotes[[i]]$votes[['-']][['groups']]) } ) ),
                                             ncol=3 ), 1,max))

> selection_vec
[1] FALSE FALSE FALSE  TRUE
于 2020-08-08T22:03:32.007 回答