How to collapse only some duplicated columns in R - r

I have a large dataset where I have a problem similar to the one below. I have something like this:
dummydf <- data.frame(country = c("USA", "USA"),
state = c("Oregon", "California"),
name = c("anne", "paul"),
family = c("stevens", "williams"),
votes = c(10, 50.2),
city = c("london", "berlin"),
age = c(10, 50),
`name...2` = c("joseph", "vincent"),
`family...2` = c("ramos", "williams"),
`votes...2` = c(15, 62),
`city...2` = c("lisbon", "berlin"),
`age...2` = c(77, 43),
`name...3` = c("johanna", "paul"),
`family...3` = c("santos", "ramos"),
`votes...3` = c(.61, 54.2),
`city...3` = c("london", "berlin"),
`age...3` = c(56, 54),
`name...4` = c("sara", "edith"),
`family...4` = c("stevens", "sanchez"),
`votes...4` = c(2.9, 54.1),
`city...4` = c("lisbon", "paris"),
`age...4` = c(20, 25),
`name...5` = c("thomas", "paul"),
`family...5` = c("santos", "ramos"),
`votes...5` = c(1.2, 5.2),
`city...5` = c("lisbon", "toronto"),
`age...5` = c(45, 80))
or maybe this is easier to understand:
country state name family votes city age name...2 family...2 votes...2 city...2 age...2 name...3 family...3 votes...3 city...3 age...3 name...4 family...4 votes...4
1 USA Oregon anne stevens 10.0 london 10 joseph ramos 15 lisbon 77 johanna santos 0.61 london 56 sara stevens 2.9
2 USA California paul williams 50.2 berlin 50 vincent williams 62 berlin 43 paul ramos 54.20 berlin 54 edith sanchez 54.1
city...4 age...4 name...5 family...5 votes...5 city...5 age...5
1 lisbon 20 thomas santos 1.2 lisbon 45
2 paris 25 paul ramos 5.2 toronto 80
So the issue here is that all the columns except country and state are multipled 5 times. Ideally I want something like this:
idealdf
name family votes city age state country
1 anne stevens 10.00 london 10 Oregon USA
2 paul williams 50.20 berlin 50 California USA
3 joseph ramos 15.00 lisbon 77 Oregon USA
4 vincent williams 62.00 berlin 43 California USA
5 johanna santos 0.61 london 56 Oregon USA
6 paul ramos 54.20 berlin 54 California USA
7 sara stevens 2.90 lisbon 20 Oregon USA
8 edith sanchez 54.10 paris 25 California USA
9 thomas santos 1.20 lisbon 45 Oregon USA
10 paul ramos 5.20 toronto 80 California USA
In the original dataset I have it multiple several hundred times and not 5 but the issue is the same. I tried a few things with pivoting and trying to clean the names with regular expressions but nothing really works well. Any pointers would be amazing - thanks!

One way could be with bringing into long form with pivot_longer. With str_remove we remove all ...1 etc..
Finally bring back in wide format using the trick with group_by and row_number():
library(dplyr)
library(tidyr)
library(stringr)
dummydf %>%
mutate(across(, as.character)) %>%
pivot_longer(-c(country, state)) %>%
mutate(name = str_remove(name, '\\...\\d+')) %>%
group_by(name) %>%
mutate(id = row_number()) %>%
pivot_wider(names_from = name, values_from = value) %>%
select(-id)
country state name family votes city age
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 USA Oregon anne stevens 10 london 10
2 USA Oregon joseph ramos 15 lisbon 77
3 USA Oregon johanna santos 0.61 london 56
4 USA Oregon sara stevens 2.9 lisbon 20
5 USA Oregon thomas santos 1.2 lisbon 45
6 USA California paul williams 50.2 berlin 50
7 USA California vincent williams 62 berlin 43
8 USA California paul ramos 54.2 berlin 54
9 USA California edith sanchez 54.1 paris 25
10 USA California paul ramos 5.2 toronto 80

Base R approach (which keeps original column types):
nam <- sub('...\\d$', '', names(dummydf))
as.data.frame(sapply(unique(nam), function(x)
unlist(dummydf[nam==x], use.names=F), simplify=F))
# country state name family votes city age
# 1 USA Oregon anne stevens 10.00 london 10
# 2 USA California paul williams 50.20 berlin 50
# 3 USA Oregon joseph ramos 15.00 lisbon 77
# 4 USA California vincent williams 62.00 berlin 43
# 5 USA Oregon johanna santos 0.61 london 56
# 6 USA California paul ramos 54.20 berlin 54
# 7 USA Oregon sara stevens 2.90 lisbon 20
# 8 USA California edith sanchez 54.10 paris 25
# 9 USA Oregon thomas santos 1.20 lisbon 45
# 10 USA California paul ramos 5.20 toronto 80
Using sapply, for each unique column name (after removing the ...n part) the appropriate subset of dummydf is coerced to a vector and the resulting list is again coerced to a data frame. The shorter elements of the list for country and state columns are recycled as necessary. use.names=F in unlist could be omitted but that would result in a warning which I'd rather avoid.

Related

Error when merging: Error in `vectbl_as_row_location()`: ! Must subset rows with a valid subscript vector. x Subscript `x` has the wrong type

I am trying to merge two dataframes in r, and this error message keeps coming up even though the variable types all should be correct.
Here is my code:
team_info <- baseballr::mlb_teams(season = 2022)
team_info_mlb <- subset(team_info, sport_name == 'Major League Baseball')
tim2 <- team_info_mlb %>%
rename('home_team' = club_name)
tim3 <- subset(tim2, select = c('team_full_name', 'home_team'))
new_pf <- baseballr::fg_park(yr = 2022)
new_pf <- subset(new_pf, select = c('home_team', '1yr'))
info_pf <- merge(tim3, new_pf, by = 'home_team')
The final line is where the problems happen. Let me know if anyone has advice.
The problem is that the data have some fancy class attributes.
> class(tim3)
[1] "baseballr_data" "tbl_df" "tbl" "data.table" "data.frame"
> class(new_pf)
[1] "baseballr_data" "tbl_df" "tbl" "data.table" "data.frame"
Just wrap them in as.data.frame(). Since both data sets have the same by variable you may omit explicit specification.
info_pf <- merge(as.data.frame(tim3), as.data.frame(new_pf))
info_pf
# home_team team_full_name 1yr
# 1 Angels Los Angeles Angels 102
# 2 Astros Houston Astros 99
# 3 Athletics Oakland Athletics 94
# 4 Blue Jays Toronto Blue Jays 106
# 5 Braves Atlanta Braves 105
# 6 Brewers Milwaukee Brewers 102
# 7 Cardinals St. Louis Cardinals 92
# 8 Cubs Chicago Cubs 103
# 9 Diamondbacks Arizona Diamondbacks 103
# 10 Dodgers Los Angeles Dodgers 98
# 11 Giants San Francisco Giants 99
# 12 Guardians Cleveland Guardians 97
# 13 Mariners Seattle Mariners 94
# 14 Marlins Miami Marlins 97
# 15 Mets New York Mets 91
# 16 Nationals Washington Nationals 97
# 17 Orioles Baltimore Orioles 108
# 18 Padres San Diego Padres 96
# 19 Phillies Philadelphia Phillies 98
# 20 Pirates Pittsburgh Pirates 101
# 21 Rangers Texas Rangers 98
# 22 Rays Tampa Bay Rays 89
# 23 Red Sox Boston Red Sox 111
# 24 Reds Cincinnati Reds 112
# 25 Rockies Colorado Rockies 112
# 26 Royals Kansas City Royals 108
# 27 Tigers Detroit Tigers 94
# 28 Twins Minnesota Twins 99
# 29 White Sox Chicago White Sox 100
# 30 Yankees New York Yankees 99

Extract out home team and away teams separated by "at" in R

I have a vector of Matchups in college basketball:
c("#34 Colorado at #36 California", "#31 Utah at #87 Stanford",
"#26 USC at #112 Wash State", "#56 UCLA at #134 Washington",
"#187 W Illinois at #116 Neb Omaha", "#222 Denver at #58 S Dakota St",
"#245 IUPUI at #170 South Dakota", "#268 Rice at #208 TX El Paso",
"#274 North Texas at #344 TX-San Ant", "#14 Iowa at #3 Purdue"
)
I'd like two separate vectors: one for the teams before at and the other for teams that appear after at. For ex) first vector would have Colorado, Utah, USC, etc and the second vector would have California, Stanford, Wash State, etc.
Notice how I don't want the # rankings. I just want the team names. I've tried str_spliting, but doesn't work too well since the spacings are all inconsistent.
We can use strsplit and split on "at" which will give us 2 parts of string and from every part we remove "#" followed by number and put it in a dataframe.
data.frame(t(sapply(strsplit(string, "\\bat\\b"),
function(x) trimws(sub("#[0-9]+", "", x)))))
# X1 X2
#1 Colorado California
#2 Utah Stanford
#3 USC Wash State
#4 UCLA Washington
#5 W Illinois Neb Omaha
#6 Denver S Dakota St
#7 IUPUI South Dakota
#8 Rice TX El Paso
#9 North Texas TX-San Ant
#10 Iowa Purdue
Or using tidyr::separate
tidyr::separate(data.frame(col = trimws(gsub("#[0-9]+", "", string))),
col, into = c("T1", "T2"), sep = "\\bat\\b")
# T1 T2
#1 Colorado California
#2 Utah Stanford
#3 USC Wash State
#4 UCLA Washington
#5 W Illinois Neb Omaha
#6 Denver S Dakota St
#7 IUPUI South Dakota
#8 Rice TX El Paso
#9 North Texas TX-San Ant
#10 Iowa Purdue
Another solution with str_extract_all()
df <- data.frame(stringsAsFactors = FALSE,
text = c("#34 Colorado at #36 California", "#31 Utah at #87 Stanford",
"#26 USC at #112 Wash State", "#56 UCLA at #134 Washington",
"#187 W Illinois at #116 Neb Omaha", "#222 Denver at #58 S Dakota St",
"#245 IUPUI at #170 South Dakota", "#268 Rice at #208 TX El Paso",
"#274 North Texas at #344 TX-San Ant", "#14 Iowa at #3 Purdue")
)
library(stringr)
library(dplyr)
df %>%
mutate(team_a = str_extract_all(text, "(?<=\\s).+(?=\\s+at)"),
team_b = str_extract_all(text, "(?<=\\d\\s)[^\\d]+$"))
#> text team_a team_b
#> 1 #34 Colorado at #36 California Colorado California
#> 2 #31 Utah at #87 Stanford Utah Stanford
#> 3 #26 USC at #112 Wash State USC Wash State
#> 4 #56 UCLA at #134 Washington UCLA Washington
#> 5 #187 W Illinois at #116 Neb Omaha W Illinois Neb Omaha
#> 6 #222 Denver at #58 S Dakota St Denver S Dakota St
#> 7 #245 IUPUI at #170 South Dakota IUPUI South Dakota
#> 8 #268 Rice at #208 TX El Paso Rice TX El Paso
#> 9 #274 North Texas at #344 TX-San Ant North Texas TX-San Ant
#> 10 #14 Iowa at #3 Purdue Iowa Purdue
Created on 2019-03-29 by the reprex package (v0.2.1)
We can do this in base R by removing substring from the 'text' column and using read.csv
read.csv(text = trimws(gsub("#\\d+", "", gsub("\\s+at\\s+", ",", df$text))),
header = FALSE, col.names = c("T1", "T2"), stringsAsFactors = FALSE)
# T1 T2
#1 Colorado California
#2 Utah Stanford
#3 USC Wash State
#4 UCLA Washington
#5 W Illinois Neb Omaha
#6 Denver S Dakota St
#7 IUPUI South Dakota
#8 Rice TX El Paso
#9 North Texas TX-San Ant
#10 Iowa Purdue

Grouping and/or Counting in R

I'm trying to 're-count' a column in R and having issues by cleaning up the data. I'm working on cleaning data by location and once I change CA to California.
all_location <- read.csv("all_location.csv", stringsAsFactors = FALSE)
all_location <- count(all_location, location)
all_location <- all_location[with(all_location, order(-n)), ]
all_location
A tibble: 100 x 2
location n
<chr> <int>
1 CA 3216
2 Alaska 2985
3 Nevada 949
4 Washington 253
5 Hawaii 239
6 Montana 218
7 Puerto Rico 149
8 California 126
9 Utah 83
10 NA 72
From the above, there's CA and California. Below I'm able to clean grep and replace CA with California. However, my issue is that it's grouping by California but shows two separate instances of California.
ca1 <- grep("CA",all_location$location)
all_location$location <- replace(all_location$location,ca1,"California")
all_location
A tibble: 100 x 2
location n
<chr> <int>
1 California 3216
2 Alaska 2985
3 Nevada 949
4 Washington 253
5 Hawaii 239
6 Montana 218
7 Puerto Rico 149
8 California 126
9 Utah 83
10 NA 72
My goal would be to combine both to a total under n.
all_location$location[substr(all_location$location, 1, 5) %in% "Calif" ] <- "California"
to make sure everything that starts with "Calif" gets made into "California"
I am assuming that maybe you have a space in the California (e.g. "California ") that is already present which is why this is happening..

removing duplicate/repeating values in the same data frame column in R

I have a weird data frame where the Player column has the names of the players. The problem is that the first name is shown twice. So Roy Sievers is RoyRoy Sievers, and I want the name to obviously be Roy Sievers.
Would anybody know how to do this?
Here is the full data frame, it's not very long:
Year Player Team Position
1 1949 RoyRoy Sievers St. Louis Browns OF
2 1950 WaltWalt Dropo Boston Red Sox 1B
3 1951 GilGil McDougald New York Yankees 3B
4 1952 HarryHarry Byrd Philadelphia Athletics P
5 1953 HarveyHarvey Kuenn Detroit Tigers SS
6 1954 BobBob Grim New York Yankees P
7 1955 HerbHerb Score Cleveland Indians P
8 1956 LuisLuis Aparicio Chicago White Sox SS
9 1957 TonyTony Kubek New York Yankees SS
10 1958 AlbieAlbie Pearson Washington Senators OF
11 1959 BobBob Allison Washington Senators OF
12 1960 RonRon Hansen Baltimore Orioles SS
13 1961 DonDon Schwall Boston Red Sox P
14 1962 TomTom Tresh New York Yankees SS
15 1963 GaryGary Peters Chicago White Sox P
16 1964 TonyTony Oliva Minnesota Twins OF
17 1965 CurtCurt Blefary Baltimore Orioles OF
18 1966 TommieTommie Agee Chicago White Sox OF
19 1967 RodRod Carew Minnesota Twins 2B
20 1968 StanStan Bahnsen New York Yankees P
21 1969 LouLou Piniella Kansas City Royals OF
22 1970 ThurmanThurman Munson New York Yankees C
23 1971 ChrisChris Chambliss Cleveland Indians 1B
24 1972 CarltonCarlton Fisk Boston Red Sox C
25 1973 AlAl Bumbry Baltimore Orioles OF
26 1974 MikeMike Hargrove Texas Rangers 1B
27 1975 FredFred Lynn Boston Red Sox OF
28 1976 MarkMark Fidrych Detroit Tigers P
29 1977 EddieEddie Murray Baltimore Orioles DH
30 1978 LouLou Whitaker Detroit Tigers 2B
31 1979* JohnJohn Castino Minnesota Twins 3B
32 1979* AlfredoAlfredo Griffin Toronto Blue Jays SS
33 1980 JoeJoe Charboneau Cleveland Indians OF
34 1981 DaveDave Righetti New York Yankees P
35 1982 CalCal Ripken Baltimore Orioles SS
36 1983 RonRon Kittle Chicago White Sox OF
37 1984 AlvinAlvin Davis Seattle Mariners 1B
38 1985 OzzieOzzie Guillén Chicago White Sox SS
39 1986 JoseJose Canseco Oakland Athletics OF
40 1987 MarkMark McGwire Oakland Athletics 1B
41 1988 WaltWalt Weiss Oakland Athletics SS
42 1989 GreggGregg Olson Baltimore Orioles P
43 1990 Sandy Alomar Jr Cleveland Indians C
44 1991 ChuckChuck Knoblauch Minnesota Twins 2B
45 1992 PatPat Listach Milwaukee Brewers SS
46 1993 TimTim Salmon California Angels OF
47 1994 BobBob Hamelin Kansas City Royals DH
48 1995 MartyMarty Cordova Minnesota Twins OF
49 1996 DerekDerek Jeter New York Yankees SS
50 1997 NomarNomar Garciaparra Boston Red Sox SS
51 1998 BenBen Grieve Oakland Athletics OF
52 1999 CarlosCarlos Beltrán Kansas City Royals OF
53 2000 KazuhiroKazuhiro Sasaki Seattle Mariners P
54 2001 IchiroIchiro Suzuki Seattle Mariners OF
55 2002 EricEric Hinske Toronto Blue Jays 3B
56 2003 ÁngelÁngel Berroa Kansas City Royals SS
57 2004 BobbyBobby Crosby Oakland Athletics SS
58 2005 HustonHuston Street Oakland Athletics P
59 2006 JustinJustin Verlander Detroit Tigers P
60 2007 DustinDustin Pedroia Boston Red Sox 2B
61 2008 EvanEvan Longoria Tampa Bay Rays 3B
62 2009 Andrew Bailey Oakland Athletics P
63 2010 NeftalíNeftalí Feliz Texas Rangers P
64 2011 JeremyJeremy Hellickson Tampa Bay Rays P
65 2012 MikeMike Trout Los Angeles Angels OF
66 2013 WilWil Myers Tampa Bay Rays OF
67 2014 JoséJosé Abreu Chicago White Sox 1B
68 2015 CarlosCarlos Correa Houston Astros SS
69 2016 MichaelMichael Fulmer Detroit Tigers P
You can fix this by finding a repeated pattern of at least three letters and replacing it with one copy like this:
gsub("(\\w{3,})\\1", "\\1", Players$Player)
If you want to overwrite the old version, just
Players$Player = gsub("(\\w{3,})\\1", "\\1", Players$Player)
G5W's answer gets you most of the way there, but would miss two-letter first names like "Al". This version relies on capitalization, and not character count:
myData$Player <- gsub('([A-Z][a-z]+)\\1', '\\1', myData$Player)
For the not so regex savvy---
library(stringr)
fun1<-function(string){
g<-str_split(g," ")
h<-str_length(m<-g[[1]][1])
l<-str_sub(m,start = 1,end = h/2)
return(paste(l,g[[1]][2]))
}
fun1(df$Player)

Making a Sankey Diagram with googleVis in R

I am trying to create a Sankey diagram in R, using the googleVis package. (The data.frame that I am using can be found below) What I want the diagram to do is go from the Type, to the Organization, then to the team (Tm), while the size represents the number of (Name) players. From what I have read, one can only three columns. I, therefore, did that using this code
BrewersDraft <- sqldf("SELECT Type, Organization, COUNT(Name) AS PLAYERS
FROM df
GROUP BY 1,2
UNION ALL
SELECT Type, (Tm) AS MLB_TEAM, COUNT(Name) AS PLAYERS
FROM df
GROUP BY 1,2")
The data now looks like this:
Type Organization
1 College/University Bradley University (Peoria, IL)
2 College/University California State University Fullerton (Fullerton, CA)
3 College/University Clemson University (Clemson, SC)
4 College/University East Tennessee State University (Johnson City, TN)
5 College/University Faulkner University (Montgomery, AL)
6 College/University Felician College (Lodi, NJ)
PLAYERS
1 1
2 1
3 1
4 1
5 1
6 1
The "Brewers" value is also in the Organization value. Then I used this code to create the Sankey Diagram:
plot(gvisSankey(BrewersDraft, from = "Type", to="Organization_Type", weight = "PLAYERS",
options = list(height=800, width=850,
sankey="{
link:{color:{fill: 'lightblue'}}}")))
The problem is that the Brewers value in the Sankey diagram is with all of the Organization variables when I want the Organization variables to flow to the Brewers variable.
It should look similar to the example on this website, https://thedatagame.com.au/2015/12/14/visualising-the-2015-nba-draft-in-r/
Only difference being that all of the Organization is only going to one team, instead of many.
Can anybody help me? Thank you, it would be much appreciated.
The original data frame.
Year Rnd OvPck RdPck Tm Name Pos
1 2016 1 5 5 Brewers Corey Ray (minors) OF
2 2016 2 46 5 Brewers Lucas Erceg (minors) 3B
3 2016 2 75 34 Brewers Mario Feliciano (minors) C
4 2016 3 82 5 Brewers Braden Webb (minors) RHP
5 2016 4 111 5 Brewers Corbin Burnes (minors) RHP
6 2016 5 141 5 Brewers Zack Brown (minors) RHP
7 2016 6 171 5 Brewers Payton Henry (minors) C
8 2016 7 201 5 Brewers Daniel Brown (minors) LHP
9 2016 8 231 5 Brewers Francisco Thomas (minors) SS
10 2016 9 261 5 Brewers Trey York (minors) 2B
11 2016 10 291 5 Brewers Blake Fox (minors) LHP
12 2016 11 321 5 Brewers Chad McClanahan (minors) 3B
13 2016 12 351 5 Brewers Trever Morrison (minors) SS
14 2016 13 381 5 Brewers Thomas Jankins (minors) RHP
15 2016 14 411 5 Brewers Gabriel Garcia (minors) C
16 2016 15 441 5 Brewers Scott Serigstad (minors) RHP
17 2016 16 471 5 Brewers Louie Crow (minors) RHP
18 2016 17 501 5 Brewers Weston Wilson (minors) 3B
19 2016 18 531 5 Brewers Cooper Hummel (minors) C
20 2016 19 561 5 Brewers Zach Clark (minors) CF
21 2016 20 591 5 Brewers Jared Horn (minors) RHP
22 2016 21 621 5 Brewers Nathan Rodriguez (minors) C
23 2016 22 651 5 Brewers Cam Roegner (minors) LHP
24 2016 23 681 5 Brewers Ronnie Gideon (minors) 1B
25 2016 24 711 5 Brewers Michael Gonzalez (minors) RHP
26 2016 25 741 5 Brewers Blake Lillis (minors) LHP
27 2016 26 771 5 Brewers Nick Roscetti (minors) SS
28 2016 27 801 5 Brewers Nick Cain (minors) RF
29 2016 28 831 5 Brewers Andrew Vernon (minors) RHP
30 2016 29 861 5 Brewers Brennan Price (minors) RHP
31 2016 30 891 5 Brewers Dalton Brown (minors) RHP
32 2016 31 921 5 Brewers Ryan Aguilar (minors) 1B
33 2016 32 951 5 Brewers Wilson Adams (minors) RHP
34 2016 33 981 5 Brewers Emerson Gibbs (minors) RHP
35 2016 34 1011 5 Brewers Matt Smith (minors) RHP
36 2016 35 1041 5 Brewers Chase Williams (minors) RHP
37 2016 36 1071 5 Brewers Parker Bean (minors) RHP
38 2016 37 1101 5 Brewers Jomar Cortes (minors) SS
39 2016 38 1131 5 Brewers Caleb Whalen (minors) CF
40 2016 39 1161 5 Brewers Jose Gomez (minors) CF
41 2016 40 1191 5 Brewers Kyle Serrano (minors) RHP
Type Organization
1 College/University University of Louisville (Louisville, KY)
2 College/University Menlo College (Atherton, CA)
3 High School Carlos Beltran Baseball Academy (Florida, PR)
4 College/University University of South Carolina (Columbia, SC)
5 College/University St. Mary's College of California (Moraga, CA)
6 College/University University of Kentucky (Lexington, KY)
7 High School Pleasant Grove HS (Pleasant Grove, UT)
8 College/University Mississippi State University (Mississippi State, MS)
9 High School Osceola HS (Kissimmee, FL)
10 College/University East Tennessee State University (Johnson City, TN)
11 College/University Rice University (Houston, TX)
12 High School Brophy College Preparatory (Phoenix, AZ)
13 College/University Oregon State University (Corvallis, OR)
14 College/University Quinnipiac College (Hamden, CT)
15 Junior College Broward Community College (Fort Lauderdale, FL)
16 College/University California State University Fullerton (Fullerton, CA)
17 High School Buena Park HS (Buena Park, CA)
18 College/University Clemson University (Clemson, SC)
19 College/University University of Portland (Portland, OR)
20 Junior College Pearl River Community College (Poplarville, MS)
21 High School Vintage HS (Napa, CA)
22 Junior College Cypress College (Cypress, CA)
23 College/University Bradley University (Peoria, IL)
24 College/University Texas A&M University (College Station, TX)
25 High School Norwalk HS (Norwalk, CT)
26 High School St. Thomas Aquinas HS (Overland Park, KS)
27 College/University University of Iowa (Iowa City, IA)
28 College/University Faulkner University (Montgomery, AL)
29 College/University North Carolina Central University (Durham, NC)
30 College/University Felician College (Lodi, NJ)
31 College/University Texas Tech University (Lubbock, TX)
32 College/University University of Arizona (Tucson, AZ)
33 College/University University of Alabama in Huntsville (Huntsville, AL)
34 College/University Tulane University (New Orleans, LA)
35 College/University Georgetown University (Washington, DC)
36 College/University Wichita State University (Wichita, KS)
37 College/University Liberty University (Lynchburg, VA)
38 High School Carlos Beltran Baseball Academy (Florida, PR)
39 College/University University of Portland (Portland, OR)
40 College/University St. Thomas University (Miami Gardens, FL)
41 College/University University of Tennessee (Knoxville, TN)
If I understand correctly you have 3 states: type, organization and team. Type is always the origin, team is the final destination and organization is at first a destination and then an origin.
In the second SQL statement you use "Type" again as the origin, when the origin should be "Organization".
Your SQL has to be modified to look like this:
BrewersDraft <- sqldf("SELECT Type, Organization, COUNT(Name) AS PLAYERS
FROM df
GROUP BY 1,2
UNION ALL
SELECT Organization, (Tm) AS MLB_TEAM, COUNT(Name) AS PLAYERS
FROM df
GROUP BY 1,2")

Resources