Reputation: 53
I have several tables with multiple columns of numeric data. The number of columns can vary. Each table has 3 columns (Player, Team, Pos) that do not have numeric data. I have come up with the following to convert every column except the 3 above to numeric but I am getting stuck on an error I can not explain.
Here is the function
qb2 <- qb %>% mutate_at(grep("^(`?!`Player|`?!`Team|`?!`Pos).*$",
colnames(.)),funs(as.numeric))
When run this way, I get NA in Player, Team & Position Trouble shooting I can get it to partially work like this
qb2 <- qb %>% mutate_at(grep("^(`?!`Player|Team|`?!`Pos).*$",
colnames(.)),funs(as.numeric))
Player and Pos show correctly, but of course Team is still NA
I am not sure why removing ?!
from Team works partially.
Let me know what I am missing that is causing the grep/exclude to fail.
Sample data
structure(list(Player = c("Matt Schaub", "Jimmy Garoppolo", "Tom Savage",
"Dan Orlovsky", "Cardale Jones", "Ryan Griffin", "Sean Mannion",
"Luke McCown", "Scott Tolzien", "Dak Prescott", "Brandon Allen",
"Garrett Grayson", "AJ McCarron", "Joe Callahan", "Matt Moore",
"Matt Barkley", "Blake Bortles", "Stephen Morris", "Sean Renfree",
"Mark Sanchez", "Jameill Showers", "Thad Lewis", "Jameis Winston",
"Kellen Clemens", "Chad Henne", "Derek Anderson", "Bryce Petty",
"Teddy Bridgewater", "Ryan Mallett", "EJ Manuel", "Nate Sudfeld",
"Joe Webb", "Carson Wentz", "Trevor Siemian", "Mike Glennon",
"Josh Johnson", "Joe Licata", "Brian Hoyer", "Geno Smith", "Joel Stave",
"Ryan Nassib", "Paxton Lynch", "Ryan Fitzpatrick", "Jake Rudock",
"Connor Cook", "Robert Griffin III", "Alex Tanney", "Brandon Doughty",
"Philip Rivers", "Blaine Gabbert", "Case Keenum", "Matthew Stafford",
"Matt Cassel", "Landry Jones", "Marquise Williams", "Colt McCoy",
"Shaun Hill", "Aaron Murray", "Dustin Vaughan", "Tyler Bray",
"Mike Bercovici", "Marcus Mariota", "Derek Carr", "Drew Stanton",
"Matt McGloin", "Kirk Cousins", "Jared Goff", "Carson Palmer",
"Drew Brees", "Nick Foles", "Cam Newton", "Alex Smith", "Russell Wilson",
"Jake Heaps", "Andy Dalton", "Austin Davis", "David Fales", "Keith Wenning",
"Brock Osweiler", "Bruce Gradkowski", "Zach Mettenberger", "Kevin Hogan",
"Jeff Driskel", "Jay Cutler", "Connor Shaw", "Jake Coker", "Zac Dysert",
"Chase Daniel", "Cody Kessler", "Logan Thomas", "Ryan Tannehill",
"McLeod Bethel-Thompson", "Jerrod Johnson", "Tyrod Taylor", "Josh McCown",
"Sam Bradford", "Matt Ryan", "Matt Simms"), Team = c("ATL", "NE",
"HOU", "DET", "BUF", "TB", "LA", "NO", "IND", "DAL", "JAX", "NO",
"CIN", "GB", "MIA", "ARI", "JAX", "IND", "ATL", "DEN", "DAL",
"SF", "TB", "SD", "JAX", "CAR", "NYJ", "MIN", "BAL", "BUF", "WAS",
"CAR", "PHI", "DEN", "TB", "BAL", "CIN", "CHI", "NYJ", "MIN",
"NYG", "DEN", "NYJ", "DET", "OAK", "CLE", "TEN", "MIA", "SD",
"SF", "LA", "DET", "TEN", "PIT", "GB", "WAS", "MIN", "KC", "PIT",
"KC", "SD", "TEN", "OAK", "ARI", "OAK", "WAS", "LA", "ARI", "NO",
"KC", "CAR", "KC", "SEA", "SEA", "CIN", "CLE", "CHI", "CIN",
"HOU", "PIT", "SD", "KC", "SF", "CHI", "CHI", "ARI", "MIA", "PHI",
"CLE", "NYG", "MIA", "PHI", "BAL", "BUF", "CLE", "PHI", "ATL",
"ATL"), Pos = c("QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB"), `
Comp` = c("10", "11", "14", "16", "11", "13",
"18", "18", "12", "10", "10", "10", "11", "16", "10", "8", "6",
"5", "5", "10", "8", "12", "7", "8", "8", "7", "7", "6", "11",
"10", "10", "9", "12", "7", "7", "8", "3", "7", "8", "8", "7",
"6", "3", "8", "7", "4", "6", "7", "2", "4", "6", "4", "3", "6",
"6", "7", "6", "3", "6", "3", "3", "5", "3", "2", "5", "5", "4",
"3", "1", "4", "5", "3", "3", "3", "4", "4", "4", "3", "4", "3",
"2", "2", "4", "3", "2", "2", "1", "4", "2", "2", "2", "1", "1",
"2", "1", "1", "0", "0"), `
Att` = c("15", "18", "24", "25",
"21", "23", "25", "27", "23", "12", "19", "16", "16", "23", "14",
"24", "7", "9", "9", "13", "16", "21", "9", "15", "9", "14",
"14", "7", "15", "18", "15", "16", "24", "12", "14", "11", "7",
"10", "14", "13", "15", "7", "4", "11", "11", "8", "10", "9",
"3", "10", "7", "6", "5", "12", "14", "11", "7", "5", "11", "9",
"6", "5", "7", "6", "11", "5", "9", "5", "1", "8", "6", "4",
"6", "10", "5", "9", "8", "8", "7", "3", "8", "6", "12", "4",
"4", "5", "2", "10", "2", "3", "4", "1", "2", "3", "4", "1",
"4", "3"), `
Pct` = c("66.7", "61.1", "58.3", "64.0", "52.4",
"56.5", "72.0", "66.7", "52.2", "83.3", "52.6", "62.5", "68.8",
"69.6", "71.4", "33.3", "85.7", "55.6", "55.6", "76.9", "50.0",
"57.1", "77.8", "53.3", "88.9", "50.0", "50.0", "85.7", "73.3",
"55.6", "66.7", "56.3", "50.0", "58.3", "50.0", "72.7", "42.9",
"70.0", "57.1", "61.5", "46.7", "85.7", "75.0", "72.7", "63.6",
"50.0", "60.0", "77.8", "66.7", "40.0", "85.7", "66.7", "60.0",
"50.0", "42.9", "63.6", "85.7", "60.0", "54.5", "33.3", "50.0",
"100.0", "42.9", "33.3", "45.5", "100.0", "44.4", "60.0", "100.0",
"50.0", "83.3", "75.0", "50.0", "30.0", "80.0", "44.4", "50.0",
"37.5", "57.1", "100.0", "25.0", "33.3", "33.3", "75.0", "50.0",
"40.0", "50.0", "40.0", "100.0", "66.7", "50.0", "100.0", "50.0",
"66.7", "25.0", "100.0", "0.0", "0.0"), `
Att/G` = c("15.0",
"18.0", "24.0", "25.0", "21.0", "23.0", "25.0", "27.0", "23.0",
"12.0", "19.0", "16.0", "16.0", "23.0", "14.0", "24.0", "7.0",
"9.0", "9.0", "13.0", "16.0", "21.0", "9.0", "15.0", "9.0", "14.0",
"14.0", "7.0", "15.0", "18.0", "15.0", "16.0", "24.0", "12.0",
"14.0", "11.0", "7.0", "10.0", "14.0", "13.0", "15.0", "7.0",
"4.0", "11.0", "11.0", "8.0", "10.0", "9.0", "3.0", "10.0", "7.0",
"6.0", "5.0", "12.0", "14.0", "11.0", "7.0", "5.0", "11.0", "9.0",
"6.0", "5.0", "7.0", "6.0", "11.0", "5.0", "9.0", "5.0", "1.0",
"8.0", "6.0", "4.0", "6.0", "10.0", "5.0", "9.0", "8.0", "8.0",
"7.0", "3.0", "8.0", "6.0", "12.0", "4.0", "4.0", "5.0", "2.0",
"10.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0",
"4.0", "3.0"), `
Yds` = c("179", "168", "168", "164", "162",
"148", "147", "143", "140", "139", "138", "133", "125", "124",
"122", "121", "105", "104", "101", "99", "99", "97", "97", "96",
"94", "93", "93", "92", "91", "91", "89", "89", "89", "88", "83",
"83", "82", "81", "79", "76", "75", "74", "72", "72", "71", "67",
"67", "66", "64", "63", "58", "58", "55", "55", "55", "53", "52",
"52", "50", "48", "46", "45", "44", "42", "41", "39", "38", "38",
"37", "37", "36", "36", "34", "33", "32", "32", "30", "28", "27",
"22", "22", "21", "20", "18", "17", "16", "16", "15", "15", "12",
"8", "7", "7", "7", "5", "3", "0", "0"), `
Avg` = c("11.9", "9.3",
"7.0", "6.6", "7.7", "6.4", "5.9", "5.3", "6.1", "11.6", "7.3",
"8.3", "7.8", "5.4", "8.7", "5.0", "15.0", "11.6", "11.2", "7.6",
"6.2", "4.6", "10.8", "6.4", "10.4", "6.6", "6.6", "13.1", "6.1",
"5.1", "5.9", "5.6", "3.7", "7.3", "5.9", "7.5", "11.7", "8.1",
"5.6", "5.8", "5.0", "10.6", "18.0", "6.5", "6.5", "8.4", "6.7",
"7.3", "21.3", "6.3", "8.3", "9.7", "11.0", "4.6", "3.9", "4.8",
"7.4", "10.4", "4.5", "5.3", "7.7", "9.0", "6.3", "7.0", "3.7",
"7.8", "4.2", "7.6", "37.0", "4.6", "6.0", "9.0", "5.7", "3.3",
"6.4", "3.6", "3.8", "3.5", "3.9", "7.3", "2.8", "3.5", "1.7",
"4.5", "4.3", "3.2", "8.0", "1.5", "7.5", "4.0", "2.0", "7.0",
"3.5", "2.3", "1.3", "3.0", "0.0", "0.0"), `
Yds/G` = c("179.0",
"168.0", "168.0", "164.0", "162.0", "148.0", "147.0", "143.0",
"140.0", "139.0", "138.0", "133.0", "125.0", "124.0", "122.0",
"121.0", "105.0", "104.0", "101.0", "99.0", "99.0", "97.0", "97.0",
"96.0", "94.0", "93.0", "93.0", "92.0", "91.0", "91.0", "89.0",
"89.0", "89.0", "88.0", "83.0", "83.0", "82.0", "81.0", "79.0",
"76.0", "75.0", "74.0", "72.0", "72.0", "71.0", "67.0", "67.0",
"66.0", "64.0", "63.0", "58.0", "58.0", "55.0", "55.0", "55.0",
"53.0", "52.0", "52.0", "50.0", "48.0", "46.0", "45.0", "44.0",
"42.0", "41.0", "39.0", "38.0", "38.0", "37.0", "37.0", "36.0",
"36.0", "34.0", "33.0", "32.0", "32.0", "30.0", "28.0", "27.0",
"22.0", "22.0", "21.0", "20.0", "18.0", "17.0", "16.0", "16.0",
"15.0", "15.0", "12.0", "8.0", "7.0", "7.0", "7.0", "5.0", "3.0",
"0.0", "0.0"), `
TD` = c("0", "0", "2", "1", "1", "0", "3", "1",
"1", "2", "0", "0", "1", "1", "1", "0", "0", "1", "0", "1", "0",
"0", "1", "0", "0", "1", "0", "1", "1", "1", "1", "0", "0", "0",
"0", "0", "0", "0", "1", "0", "0", "0", "0", "1", "0", "0", "0",
"0", "1", "1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "2", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"1", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0"),
`
Int` = c("0", "0", "0", "1", "0", "2", "1", "1", "0", "0",
"1", "1", "0", "0", "1", "1", "0", "0", "0", "1", "0", "0",
"0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "1", "0",
"1", "0", "1", "1", "0", "1", "2", "0", "0", "0", "0", "1",
"0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0",
"0", "0", "1", "0", "0", "1", "0", "0", "1", "0", "0", "0",
"0", "0", "1", "0", "0", "0", "0", "1", "0", "0", "1", "1",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0"), `
1st` = c("5", "7", "10", "10", "6",
"6", "10", "7", "5", "7", "4", "6", "5", "9", "5", "7", "4",
"3", "3", "5", "2", "6", "5", "4", "5", "5", "5", "4", "5",
"4", "5", "3", "2", "3", "4", "5", "3", "2", "4", "6", "4",
"4", "2", "3", "2", "2", "2", "3", "2", "2", "2", "3", "2",
"1", "4", "1", "4", "2", "2", "2", "2", "1", "2", "2", "2",
"2", "3", "2", "1", "2", "3", "2", "3", "3", "2", "2", "1",
"1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "0", "1",
"0", "0", "0", "0", "0", "0", "0", "0", "0"), `
1st%` = c("33.3",
"38.9", "41.7", "40.0", "28.6", "26.1", "40.0", "25.9", "21.7",
"58.3", "21.1", "37.5", "31.3", "39.1", "35.7", "29.2", "57.1",
"33.3", "33.3", "38.5", "12.5", "28.6", "55.6", "26.7", "55.6",
"35.7", "35.7", "57.1", "33.3", "22.2", "33.3", "18.8", "8.3",
"25.0", "28.6", "45.5", "42.9", "20.0", "28.6", "46.2", "26.7",
"57.1", "50.0", "27.3", "18.2", "25.0", "20.0", "33.3", "66.7",
"20.0", "28.6", "50.0", "40.0", "8.3", "28.6", "9.1", "57.1",
"40.0", "18.2", "22.2", "33.3", "20.0", "28.6", "33.3", "18.2",
"40.0", "33.3", "40.0", "100.0", "25.0", "50.0", "50.0",
"50.0", "30.0", "40.0", "22.2", "12.5", "12.5", "14.3", "33.3",
"12.5", "16.7", "0.0", "25.0", "25.0", "20.0", "50.0", "0.0",
"50.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0"), `
Lng` = c("68", "56", "24", "28T", "40",
"25", "16", "20", "42T", "32T", "27", "28", "40", "18", "51T",
"30", "45", "57T", "48", "32T", "47", "14", "26T", "17",
"36", "23", "21", "49T", "19T", "26", "28", "35", "19", "22",
"30", "29", "47", "28", "18", "22", "24", "22", "43", "27T",
"32", "49", "23", "24", "44T", "43T", "20", "30", "24", "29T",
"16", "19", "18", "25", "16", "23", "19", "31", "22", "32",
"19T", "11", "14", "30", "37", "19", "10", "20", "12", "13",
"9", "19", "13", "14", "12", "9", "14", "15", "11", "12",
"13", "13", "16T", "9", "10T", "7", "7", "7", "7", "5", "5",
"3", "--", "--"), `
20+` = c("2", "2", "4", "3", "3", "2",
"0", "1", "2", "2", "2", "3", "2", "0", "2", "2", "1", "1",
"2", "1", "1", "0", "2", "0", "1", "1", "2", "2", "0", "1",
"2", "1", "0", "2", "2", "1", "2", "2", "0", "1", "1", "1",
"2", "1", "1", "1", "1", "1", "2", "1", "1", "1", "1", "1",
"0", "0", "0", "1", "0", "2", "0", "1", "1", "1", "0", "0",
"0", "1", "1", "0", "0", "1", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0"), `
40+` = c("2",
"1", "0", "0", "1", "0", "0", "0", "1", "0", "0", "0", "1",
"0", "1", "0", "1", "1", "1", "0", "1", "0", "0", "0", "0",
"0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "1",
"0", "0", "0", "0", "0", "1", "0", "0", "1", "0", "0", "1",
"1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0"), `
Sck` = c("0", "2", "2", "0", "1", "1", "0", "2",
"0", "1", "1", "0", "1", "1", "3", "0", "1", "0", "0", "0",
"1", "1", "1", "0", "1", "1", "2", "1", "0", "1", "3", "2",
"0", "0", "1", "1", "1", "3", "0", "0", "4", "3", "1", "0",
"0", "0", "2", "1", "0", "0", "0", "1", "1", "1", "1", "0",
"0", "0", "3", "0", "0", "0", "0", "0", "0", "0", "1", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "2", "0", "1", "0",
"2", "0", "0", "2", "0", "1", "0", "4", "3", "0", "0", "0",
"0", "0", "1", "0", "0", "0"), `
Rate` = c("107.4", "91.9",
"107.6", "79.4", "93.8", "39.8", "109.5", "76.6", "85.4",
"154.5", "54.3", "62.8", "112.8", "97.0", "92.0", "33.5",
"118.8", "133.6", "95.1", "91.5", "69.5", "68.9", "148.6",
"73.2", "110.2", "95.2", "71.4", "158.3", "82.9", "88.0",
"104.6", "72.1", "41.8", "81.2", "38.7", "94.1", "47.0",
"54.6", "97.0", "45.7", "22.2", "110.7", "116.7", "120.3",
"82.0", "39.1", "80.0", "97.2", "149.3", "95.0", "101.2",
"97.9", "97.9", "90.6", "24.4", "75.2", "97.6", "95.4", "66.5",
"52.1", "36.1", "104.2", "64.0", "19.4", "95.1", "99.2",
"17.1", "83.8", "118.8", "63.0", "91.7", "102.1", "27.8",
"40.8", "93.3", "53.9", "59.4", "8.3", "65.8", "97.2", "0.0",
"4.9", "42.4", "83.3", "61.5", "48.8", "116.7", "47.9", "137.5",
"74.3", "56.2", "95.8", "58.3", "70.1", "39.6", "79.2", "39.6",
"39.6")), .Names = c("Player", "Team", "Pos", "\nComp", "\nAtt",
"\nPct", "\nAtt/G", "\nYds", "\nAvg", "\nYds/G", "\nTD", "\nInt",
"\n1st", "\n1st%", "\nLng", "\n20+", "\n40+", "\nSck", "\nRate"
), class = "data.frame", row.names = c(NA, 98L))
Upvotes: 1
Views: 367
Reputation: 2085
The NA's are coming from the "--" and "T":
qb[,-c(1:3)] <- apply(qb[,-c(1:3)], 2, as.numeric)
The one column has several values that are not numeric:
Lng = c("68", "56", "24", "28T", "40",
"25", "16", "20", "42T", "32T", "27", "28", "40", "18", "51T",
"30", "45", "57T", "48", "32T", "47", "14", "26T", "17",
"36", "23", "21", "49T", "19T", "26", "28", "35", "19", "22",
"30", "29", "47", "28", "18", "22", "24", "22", "43", "27T",
"32", "49", "23", "24", "44T", "43T", "20", "30", "24", "29T",
"16", "19", "18", "25", "16", "23", "19", "31", "22", "32",
"19T", "11", "14", "30", "37", "19", "10", "20", "12", "13",
"9", "19", "13", "14", "12", "9", "14", "15", "11", "12",
"13", "13", "16T", "9", "10T", "7", "7", "7", "7", "5", "5",
"3", "--", "--"),
Ultimately you want this:
qb[,-c(1:3)] <- apply(qb[,-c(1:3)], 2, function(x) gsub("--|T", "", x))
qb[,-c(1:3)] <- apply(qb[,-c(1:3)], 2, as.numeric)
Upvotes: 1
Reputation: 214957
Here is an option using one_of
function from dplyr
, one_of
accepts a character vector for the column names that can be excluded from the mutation by negating the selection with a -
in front:
df %>%
mutate_at(vars(-one_of(c("Player", "Team", "Pos"))), funs(as.numeric)) %>%
lapply(class)
# $Player
# [1] "character"
# $Team
# [1] "character"
# $Pos
# [1] "character"
# $`\nComp`
# [1] "numeric"
# $`\nAtt`
# [1] "numeric"
# other numeric columns here...
df %>%
mutate_at(vars(-one_of(c("Player", "Team", "Pos"))), funs(as.numeric)) %>%
head()
# Player Team Pos \nComp \nAtt \nPct \nAtt/G \nYds \nAvg \nYds/G
#1 Matt Schaub ATL QB 10 15 66.7 15 179 11.9 179
#2 Jimmy Garoppolo NE QB 11 18 61.1 18 168 9.3 168
#3 Tom Savage HOU QB 14 24 58.3 24 168 7.0 168
#4 Dan Orlovsky DET QB 16 25 64.0 25 164 6.6 164
#5 Cardale Jones BUF QB 11 21 52.4 21 162 7.7 162
#6 Ryan Griffin TB QB 13 23 56.5 23 148 6.4 148
# \nTD \nInt \n1st \n1st% \nLng \n20+ \n40+ \nSck \nRate
#1 0 0 5 33.3 68 2 2 0 107.4
#2 0 0 7 38.9 56 2 1 2 91.9
#3 2 0 10 41.7 24 4 0 2 107.6
#4 1 1 10 40.0 NA 3 0 0 79.4
#5 1 0 6 28.6 40 3 1 1 93.8
#6 0 2 6 26.1 25 2 0 1 39.8
A smaller example:
df <- data.frame(a = c("x", "y", "z"), b = c("m", "n", "l"), c = 1:3, d = 2:4)
df
# a b c d
# 1 x m 1 2
# 2 y n 2 3
# 3 z l 3 4
df %>% mutate_at(vars(-one_of(c("a", "b"))), funs(.*2))
# a b c d
# 1 x m 2 4
# 2 y n 4 6
# 3 z l 6 8
Upvotes: 2
Reputation: 121568
I would do the conversion in the long format, that I go again to the wide format:
library(data.table)
setDT(dx)
dcast(melt(dx,id=c("Player","Team", "Pos"))[,value:=as.numeric(value)],
Player+Team+Pos~variable)
where dx
is one of your data.frame. This can be easily generlised to many data.frames but it depends if you want to have the final result as a single data.table that aggregates all the data.frames or not.
Upvotes: 1