mardi 11 août 2020

Extract an information from a sublist thanks to conditions

I have a data.frame that looks like this (data available here : https://github.com/JMcrocs/MEPVote/blob/master/MEP_ID_EPG.rds)

head(MEP_ID_EPG)
   mepid     EPG
1 197701 GUE.NGL
2 197533 GUE.NGL
3 197521 GUE.NGL

and a large list of 2336 lists (data : https://github.com/JMcrocs/MEPVote/blob/master/AllVotes.rds)

str(AllVotes, max.level = 7, list.len = 5)
List of 2336
 $ :List of 7
  ..$ votes  :List of 3
  .. ..$ +:List of 2
  .. .. ..$ total : num 83
  .. .. ..$ groups:List of 6
  .. .. .. ..$ GUE/NGL  :List of 23
  .. .. .. .. ..$ : Named num 197701
  .. .. .. .. .. ..- attr(*, "names")= chr "mepid"
  .. .. .. .. ..$ : Named num 197533
  .. .. .. .. .. ..- attr(*, "names")= chr "mepid"
  .. ..$ -:List of 2
  .. .. ..$ total : num 142
  .. .. ..$ groups:List of 8
  .. .. .. ..$ ECR      :List of 27
  .. .. .. .. ..$ : Named num 198096
  .. .. .. .. .. ..- attr(*, "names")= chr "mepid"
  .. .. .. .. ..$ : Named num 197467 
  .. ..$ 0:List of 2
  .. .. ..$ total : num 72
  .. .. ..$ groups:List of 4
  .. .. .. ..$ ID       :List of 3
  .. .. .. .. ..$ : Named num 197480
  .. .. .. .. .. ..- attr(*, "names")= chr "mepid"
  .. .. .. .. ..$ : Named num 197482

My goal is to add a "+", a "-" or a "0" in the row of the MEP (mepid) in MEP_ID_EPG if he voted yea("+"), nay("-") or other ("0") if NA or he voted ("0"). In a more complex language, it should look like this

if( MEP_ID_EPG$mepid is in a sublist of AllVotes[[x]]$votes$'+'

then MEP_ID_EPG$[[x]] == '+')

if(MEP_ID_EPG$mepid is in a sublist of AllVotes[[x]]$votes$'-'

then MEP_ID_EPG$[[x]] == '-')

else('0')

and the outcome should look like this

head(MEP_ID_EPG)
   mepid     EPG      1     2  ... 2336
1 197701 GUE.NGL     +      +  ... +
2 197533 GUE.NGL     0     +   ... 0
3 197521 GUE.NGL      -     0  ... -

Can someone help me, please?

Thank you in advance !

Aucun commentaire:

Enregistrer un commentaire