Search code examples
rdataframereshapedcast

Turn unique values into new columns and check for all other values


Thanks to @hrbrmstr I got a real good solution for the "Scraping a xml document (nested url-structure)" problem.

But as a result from that insight, it emerged another problem:

Now I have got the following data-structure...

 $Vorname   : chr  "Jan" "Jan" "Jan" "Jan" ...
 $ Nachname  : chr  "Aken" "Aken" "Aken" "Aken" ...
 $ ID        : chr  "1627" "1627" "1627" "1627" ...
 $ Fraktion  : chr  "Die Linke" "Die Linke" "Die Linke" "Die Linke" ...
 $ Reli      : chr  "" "" "" "" ...
 $ Geschlecht: chr  "Männlich" "Männlich" "Männlich" "Männlich" ...
 $ Auss_ord  : chr  "Auswärtiger Ausschuss" "Gremium nach § 23c Absatz 8 des Zollfahndungsdienstgesetzes" "Verteidigungsausschuss" "Unterausschuss Abrüstung, Rüstungskontrolle und Nichtverbreitung" ...

The only variable which actually variegate is "Auss_ord" which contains as value the different commission for MPs.

Now, what I want to achive is to switch from the current long version to a wide version of the dataframe.

For every unique value (certain text string) in "Auss_ord" there should be a new column and also be checked if it is "True" weather a another object has got same values.

So that...

     Vorname   Nachname    ID  Fraktion               Reli Geschlecht                                                         Auss_ord
     <chr>      <chr> <chr>     <chr>              <chr>      <chr>                                                            <chr>
1      Jan       Aken  1627 Die Linke                      Männlich                                            Auswärtiger Ausschuss
2      Jan       Aken  1627 Die Linke                      Männlich      Gremium nach § 23c Absatz 8 des Zollfahndungsdienstgesetzes
3      Jan       Aken  1627 Die Linke                      Männlich                                           Verteidigungsausschuss
4      Jan       Aken  1627 Die Linke                      Männlich Unterausschuss Abrüstung, Rüstungskontrolle und Nichtverbreitung
5  Stephan     Albani  1769   CDU/CSU                      Männlich    Ausschuss für Bildung, Forschung und Technikfolgenabschätzung
6  Stephan     Albani  1769   CDU/CSU                      Männlich                                         Ausschuss für Gesundheit
7   Katrin Albsteiger  1770   CDU/CSU römisch-katholisch   Weiblich                                                 Schriftführer/in
8   Katrin Albsteiger  1770   CDU/CSU römisch-katholisch   Weiblich         Ausschuss für die Angelegenheiten der Europäischen Union

...should be turned into...

Example Dataframe (wide version)

I tried reshape operations like

d1 <- dcast(df, Vorname ~ Nachname ~ ID ~ Fraktion ~ Reli, value.var="ausord")

or

d2 <- reshape(df, idvar = "ID", timevar = "Auss_ord", direction = "wide")

...but I do not get any proper results, besides of the (for me) really tricky implementation of checking the uniqueness of a value


Solution

  • You could do:

    dcast(df,Vorname+Nachname+ID+Fraktion+Reli+Geschlecht~Auss_ord,length)
    

    length will allow the resulting dataframe to be filled with 0/1 for the columns generated with "Auss_ord"