2016-08-04 13 views
1

Ich habe Datenrahmen df wie folgend:Split-Datenrahmen durch denselben Wert/Text und dann verschmelzen vertikal

df$x1 df$x2  df$x3 df$x4  df$x5 df$x6   
Acti  Acti  Arthrex Arthrex Aflac Aflac  
    4   5  3   1  2   5 
    1   3  5   2  1   4   

Ich wünsche den Datenrahmen von Text in der ersten Reihe zu spalten und dann vertikal verschmelzen. Meine gewünschte Ausgabe wäre wie folgt:

df$x1 df$x2   
Acti  Acti   
    4   5  
    1   3  
Arthrex Arthrex 
    3   1 
    5   2 
Aflac Aflac 
    2   5 
    1   4 

Vielen Dank für Ihre Hilfe.

Antwort

2

Hier ist eine Lösung mit dplyr (vorausgesetzt, Ihre Daten df genannt):

library(dplyr) 

t(df) %>% # transpose the data so that the first row is now the first col 
    as_data_frame() %>% # convert back to data.frame from matrix 
    group_by(V1) %>% # group the data by the first column 
    do(as_data_frame(t(.))) # transpose each group back to the correct orientation 

dplyr::do können Sie auf jede Gruppe beliebige Operationen ausführen dann bindet Reihe die Ergebnisse wieder zusammen in einen data.frame.

+0

Ihr Code produziert Fehler. Es produziert 'Fehler: is.list (x) ist nicht wahr '. – Santosh

+0

Ich habe 'as.data.frame' anstelle von' as_data_frame' verwendet und es hat perfekt funktioniert. – Santosh

Verwandte Themen