2017-08-31 1 views
1

Wie kann ich die folgenden tibble konvertieren, um das endgültige Ergebnis mit dplyr?Geeignete Alternative für ddply-Funktion

> group_by(hth, team) %>% arrange(team) 
Source: local data frame [26 x 14] 
Groups: team [13] 

    team CSK DC DD GL KKR KTK KXIP MI PW RCB RPSG 
    <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 
1 CSK  0  8 11  0 11  2  9 10  4 10  0 
2 CSK  0  2  5  0  5  0  8 12  2  9  0 
3  DC  2  0  8  0  2  1  7  5  3  8  0 
4  DC  8  0  3  0  7  0  3  5  1  3  0 
5  DD  5  3  0  0  7  2  8  5  2 10  2 
6  DD 11  8  0  2 10  0 10 13  4  7  0 
7  GL  0  0  2  0  0  0  0  0  0  1  0 
8  GL  0  0  0  0  2  0  2  2  0  2  2 
9 KKR  5  7 10  2  0  0  5 10  3 15  0 
10 KKR 11  2  7  0  0  2 14  8  2  3  2 
# ... with 16 more rows, and 2 more variables: RR <dbl>, SH <dbl> 
> 

Ich verwendete plyrs ddply-Funktion und konnte das Ergebnis erzielen.

> ddply(hth, .(team), function(x) colSums(x[,-1], na.rm = TRUE)) 
    team CSK DC DD GL KKR KTK KXIP MI PW RCB RPSG RR SH 
1 CSK 0 10 16 0 16 2 17 22 6 19 0 17 6 
2 DC 10 0 11 0 9 1 10 10 4 11 0 9 0 
3 DD 16 11 0 2 17 2 18 18 6 17 2 16 8 
4 GL 0 0 2 0 2 0 2 2 0 3 2 0 3 
5 KKR 16 9 17 2 0 2 19 18 5 18 2 15 9 
6 KTK 2 1 2 0 2 0 1 1 1 2 0 2 0 
7 KXIP 17 10 18 2 19 1 0 18 6 18 2 15 8 
8 MI 22 10 18 2 18 1 18 0 6 19 2 16 8 
9 PW 6 4 6 0 5 1 6 6 0 5 0 5 2 
10 RCB 19 11 17 3 18 2 18 19 5 0 2 16 9 
11 RPSG 0 0 2 2 2 0 2 2 0 2 0 0 2 
12 RR 17 9 16 0 15 2 15 16 5 16 0 0 7 
13 SH 6 0 8 3 9 0 8 8 2 9 2 7 0 
> 

Wie erreichen Sie das gleiche mit nur dplyr Funktionen?

+1

'group_by (hth, Team)%>% summarise_all (Summe, na.rm = TRUE)'? –

+0

Danke. Der Code hat funktioniert. –

Antwort

0

Sieht aus wie Sie durch team und Summieren der Spalten gruppieren, in dplyr:

library(dplyr) 

hth %>% 
    group_by(team) %>% 
    summarise_all(funs(sum), na.rm = TRUE) 
+0

Arbeitete wie ein Charme :). Danke –

+0

@AnirudhMurali Kein Problem, und willkommen bei StackOverflow! Wenn die Antwort Ihnen bei Ihrem Problem geholfen hat, akzeptieren Sie es, damit andere wissen, dass es funktioniert hat (klicken Sie auf das Häkchen auf der linken Seite). – Paolo