I have some data that I want to sort according to a column within the row
which looks like this:
Rk Player Pos Age Tm G MP PER TS. X3PAr
1 492 Tyler Zeller C 25 BOS 82 1731 18.9 0.594 0.000
2 491 Cody Zeller C 22 CHO 62 1487 14.1 0.530 0.003
3 490 Thaddeus Young PF 26 TOT 76 2434 15.7 0.507 0.119
4 490 Thaddeus Young PF 26 MIN 48 1605 15.0 0.491 0.101
5 490 Thaddeus Young PF 26 BRK 28 829 17.1 0.539 0.153
6 489 Nick Young SG 29 LAL 42 1000 14.2 0.520 0.473
What Im trying to do is, as I iterate through the row, assign the row to a dataframe named after the 'Tm' (team) value. So during the first iteration if it does not exist, create the dataframe named BOS
. The next time it sees a row with Tm
column equal to BOS
assign the row to the dataframe BOS
.
Here is what I have so far:
for (i in 1:nrow(allPlayers)){
row <- allPlayers[i,]
teamName <- toString(allPlayers[i,'Tm'])
if(!exists(teamName)){
assign(teamName,data.frame())
}
assign(teamName,rbind(teamName,row))
}
But the only value that I get in each dataframe is the row from the last iteration for that team.
What I want would be multiple dataframes with all of their according players like so
BOS
492 Tyler Zeller C 25 BOS 82 1731 18.9 0.594 0
488 James Young SG 19 BOS 31 332 8.5 0.457 0.647
485 Brandan Wright PF 27 BOS 8 86 15 0.571 0
ATL
431 Jeff Teague PG 26 ATL 73 2228 20.6 0.566 0.233
404 Thabo Sefolosha SF 30 ATL 52 976 13.8 0.506 0.313
403 Mike Scott PF 26 ATL 68 1123 15.1 0.543 0.424
401 Dennis Schroder PG 21 ATL 77 1516 15.7 0.516 0.223