dataframe - I want to generate 8 combinations of names from a column in an R data frame based on conditions from other columns in the same data frame -


i have data frame 20 players 4 different teams (5 players per team), each assigned salary fantasy draft. able create combinations of 8 players salaries equal or less 10000 & total points greater x excluding combinations contains 4 or more players same team.

here data frame looks like:

       team      player    k   d       lh points salary    pps   4     atn  exoticdeer  6.1 3.3  6.4 306.9 22.209   1622 1.3692   2     atn     supreme  6.8 5.3  7.1 229.4 21.954   1578 1.3913   1     atn        sasu  3.6 6.4 11.0  95.7 19.357   1244 1.5560   3     atn el lisash 2  2.6 6.1  7.9  29.7 12.037    998 1.2061   5     atn       nisha  2.7 5.6  7.5  48.2 12.282    955 1.2861   11     cl swiftending  6.0 5.8  7.8 360.5 22.285   1606 1.3876   13     cl     pajkatt 13.3 7.5  9.3 326.8 37.248   1489 2.5015   15     cl  sexybamboe  6.3 8.5  9.3 168.0 20.660   1256 1.6449   14     cl         egm  2.8 6.0 13.5  78.8 21.988    989 2.2233   12     cl       saksa  2.5 6.5 10.5  59.8 15.898    967 1.6441   51 dbears         ace  7.0 3.4  6.9 195.6 23.596   1578 1.4953   31 dbears    hestejoe  5.4 5.4  6.1 176.7 16.927   1512 1.1195   61 dbears      miggel  2.8 6.8 11.0 141.8 17.818   1212 1.4701   21 dbears        noia  3.0 6.0  8.0  36.1 13.161    970 1.3568   41 dbears        ryze  2.7 4.7  6.7  74.6 12.166    937 1.2984   8      gb keyser soze  6.0 5.0  5.6 316.0 19.120   1602 1.1935   9      gb      madara  5.4 5.3  6.6 334.5 19.405   1577 1.2305   10     gb     skylark  1.8 5.3  7.0  71.8 10.218   1266 0.8071   7      gb         mnt  2.3 5.9  6.1  85.6  9.316   1007 0.9251   6      gb   skanks224  1.4 7.6  7.4  52.5  7.565    954 0.7930 

i following general concept described in post: i want generate combinations of 5 names column in r data frame, values in different column add number or less

tweaking code suit needs. have far:

## make list of combinations of 8 of player, points , salary xx <- with(fantasyplayers, lapply(list(as.character(player), points, salary), combn,     8)) ## convert names string,  ## find column sums of others, ## set names yy <- setnames( lapply(xx, function(x) {     if(typeof(x) == "character") apply(x, 2, tostring) else colsums(x) }), names(fantasyplayers)[c(2, 7, 8)] ) ## coerce data.frame newdf <- as.data.frame(yy) 

using above code able generate possibly lineups of 8 players , subset various criteria (total salary , number of points), struggling when comes excluding lineups there more 3 players same team.

i imagine lineups need excluded newdf don't know begin in doing that.

here dput results:

structure(list(team = c("atn", "atn", "atn", "atn", "atn", "cl",  "cl", "cl", "cl", "cl", "dbears", "dbears", "dbears", "dbears",  "dbears", "gb", "gb", "gb", "gb", "gb"), player = structure(c(2l,  5l, 4l, 1l, 3l, 15l, 12l, 14l, 11l, 13l, 16l, 18l, 19l, 20l,  21l, 6l, 7l, 10l, 8l, 9l), .label = c("el lisash 2", "exoticdeer",  "nisha", "sasu", "supreme", "keyser soze", "madara", "mnt", "skanks224",  "skylark", "egm", "pajkatt", "saksa", "sexybamboe", "swiftending",  "ace", "druidzozoneshoc", "hestejoe", "miggel", "noia", "ryze" ), class = "factor"), k = c(6.1, 6.8, 3.6, 2.6, 2.7, 6, 13.3,  6.3, 2.8, 2.5, 7, 5.4, 2.8, 3, 2.7, 6, 5.4, 1.8, 2.3, 1.4), d = c(3.3,  5.3, 6.4, 6.1, 5.6, 5.8, 7.5, 8.5, 6, 6.5, 3.4, 5.4, 6.8, 6,  4.7, 5, 5.3, 5.3, 5.9, 7.6), = c(6.4, 7.1, 11, 7.9, 7.5, 7.8,  9.3, 9.3, 13.5, 10.5, 6.9, 6.1, 11, 8, 6.7, 5.6, 6.6, 7, 6.1,  7.4), lh = c(306.9, 229.4, 95.7, 29.7, 48.2, 360.5, 326.8, 168,  78.8, 59.8, 195.6, 176.7, 141.8, 36.1, 74.6, 316, 334.5, 71.8,  85.6, 52.5), points = c(22.209, 21.954, 19.357, 12.037, 12.282,  22.285, 37.248, 20.66, 21.988, 15.898, 23.596, 16.927, 17.818,  13.161, 12.166, 19.12, 19.405, 10.218, 9.316, 7.565), salary = c(1622,  1578, 1244, 998, 955, 1606, 1489, 1256, 989, 967, 1578, 1512,  1212, 970, 937, 1602, 1577, 1266, 1007, 954), pps = c(1.3692,  1.3913, 1.556, 1.2061, 1.2861, 1.3876, 2.5015, 1.6449, 2.2233,  1.6441, 1.4953, 1.1195, 1.4701, 1.3568, 1.2984, 1.1935, 1.2305,  0.8071, 0.9251, 0.793)), .names = c("team", "player", "k", "d",  "a", "lh", "points", "salary", "pps"), class = "data.frame", row.names = c("4",  "2", "1", "3", "5", "11", "13", "15", "14", "12", "51", "31",  "61", "21", "41", "8", "9", "10", "7", "6")) 

here's 1 way:

splt.names <- strsplit(as.character(newdf$player), ", ") indices <- lapply(splt.names, function(x) match(x, fantasyplayers$player)) exclude <- lapply(indices, function(x) any(table(fantasyplayers$team[x]) > 3)) newdf2 <- newdf[!unlist(exclude), ] 

first split player column comma. match player names fantasy players player name column. indices, can main work any(table(fantasyplayers$team[x]) > 3). check of team counts exceed three, indicate 3 or more players same team.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -