我已经苦苦挣扎了两天,想找到一种从嵌套列表中创建特定矩阵的方法
首先,如果我没有正确解释我的问题,我很抱歉我是 StackOverflow* 和 R(以及编程......)的一周新手!
我使用您可以在那里找到的文件:
- 原文链接:https ://parltrack.org/dumps/ep_mep_activities.json.lz
- 我在这里解压:https ://wetransfer.com/downloads/701b7ac5250f451c6cb26d29b41bd88020200808183632/bb08429ca5102e3dc277f2f44d08f82220200808183652/666973
- 前 3 个列表和最后一个(共 23905 个)过去:https ://pastebin.com/Kq7mjis5
使用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 以来,我一直在积极阅读这个网站!