There are several ways you can solve this problem with R. Here we will discuss solutions using SQL, R base and tidyverse.
Download the MLB data and set the working directory to the folder with the file. Read the CSV file into R:
df <- read.csv(file = "MLB_cleaned.csv", stringsAsFactors = TRUE)
Look at the first 10 rows.
head(df, n = 10)
## First.Name Last.Name Team Position Height.inches. Weight.pounds.
## 1 Jeff Mathis ANA Catcher 72 180
## 2 Mike Napoli ANA Catcher 72 205
## 3 Jose Molina ANA Catcher 74 220
## 4 Howie Kendrick ANA First Baseman 70 180
## 5 Kendry Morales ANA First Baseman 73 220
## 6 Casey Kotchman ANA First Baseman 75 210
## 7 Robb Quinlan ANA First Baseman 73 200
## 8 Shea Hillenbrand ANA First Baseman 73 211
## 9 Terry Evans ANA Outfielder 75 200
## 10 Reggie Willits ANA Outfielder 71 185
## Age
## 1 23.92
## 2 25.33
## 3 31.74
## 4 23.64
## 5 23.70
## 6 24.02
## 7 29.95
## 8 31.59
## 9 25.11
## 10 25.75
Check data types (you can also see this in the Environment pane in RStudio)
str(df)
## 'data.frame': 1034 obs. of 7 variables:
## $ First.Name : Factor w/ 435 levels "A.J.","Aaron",..: 209 293 235 182 247 65 344 376 389 333 ...
## $ Last.Name : Factor w/ 849 levels "Aardsma","Abercrombie",..: 488 551 525 401 530 417 626 356 241 828 ...
## $ Team : Factor w/ 30 levels "ANA","ARZ","ATL",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Position : Factor w/ 9 levels "Catcher","Designated Hitter",..: 1 1 1 3 3 3 3 3 4 4 ...
## $ Height.inches.: int 72 72 74 70 73 75 73 73 75 71 ...
## $ Weight.pounds.: int 180 205 220 180 220 210 200 211 200 185 ...
## $ Age : num 23.9 25.3 31.7 23.6 23.7 ...
Note that this shows how many players (observations) and columns (variables) we have. Names, teams and positions are codes as factors because they are nominal labels.
Look at the summary
summary(df)
## First.Name Last.Name Team Position
## Jason : 27 Johnson : 9 NYM : 38 Relief Pitcher :315
## Chris : 26 Perez : 7 ATL : 37 Starting Pitcher:221
## Mike : 26 Gonzalez : 6 DET : 37 Outfielder :194
## Scott : 24 Hernandez: 6 OAK : 37 Catcher : 76
## Ryan : 23 Jones : 6 BOS : 36 Second Baseman : 58
## Matt : 19 Ramirez : 6 CHC : 36 First Baseman : 55
## (Other):889 (Other) :994 (Other):813 (Other) :115
## Height.inches. Weight.pounds. Age
## Min. :67.0 Min. :150.0 Min. :20.90
## 1st Qu.:72.0 1st Qu.:187.0 1st Qu.:25.44
## Median :74.0 Median :200.0 Median :27.93
## Mean :73.7 Mean :201.7 Mean :28.74
## 3rd Qu.:75.0 3rd Qu.:215.0 3rd Qu.:31.23
## Max. :83.0 Max. :290.0 Max. :48.52
##
Looks like the data was read correctly
In SQL we would do something like this
SELECT * FROM Player ORDER BY Age DESC LIMIT 3
R can directly create and use a SQLite database. You have to install
the package RSQLite
. Here is a some more info on SQLite in R.
Create an in-memory RSQLite database. Note: if you use a file name
instead of ":memory:"
then the created database will be
available later as this file.
library(RSQLite)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
Import df
into a database table called Player
dbWriteTable(con, name = "Player", value = df)
dbListTables(con)
## [1] "Player"
Look at the table
head(dbReadTable(con, name = "Player"))
## First.Name Last.Name Team Position Height.inches. Weight.pounds. Age
## 1 Jeff Mathis ANA Catcher 72 180 23.92
## 2 Mike Napoli ANA Catcher 72 205 25.33
## 3 Jose Molina ANA Catcher 74 220 31.74
## 4 Howie Kendrick ANA First Baseman 70 180 23.64
## 5 Kendry Morales ANA First Baseman 73 220 23.70
## 6 Casey Kotchman ANA First Baseman 75 210 24.02
Execute a query and fetch the results
res <-
dbSendQuery(con, statement = "SELECT * FROM Player ORDER BY Age DESC LIMIT 3")
answer <- dbFetch(res)
answer
## First.Name Last.Name Team Position Height.inches. Weight.pounds.
## 1 Julio Franco NYM First Baseman 73 188
## 2 Jamie Moyer PHI Starting Pitcher 72 175
## 3 Randy Johnson ARZ Starting Pitcher 82 231
## Age
## 1 48.52
## 2 44.28
## 3 43.47
Clean up result and disconnect from the database. The database will be destroyed since we have created a in-memory db.
dbClearResult(res)
dbDisconnect(con)
See: R Base Cheatsheet
Get the age column and sort the values.
age <- df$Age
age
## [1] 23.92 25.33 31.74 23.64 23.70 24.02 29.95 31.59 25.11 25.75 27.51 28.66
## [13] 29.10 31.06 32.51 34.67 24.32 25.14 28.09 30.29 31.61 33.31 36.40 37.36
## [25] 23.13 32.33 24.14 24.41 25.71 28.35 30.89 33.77 37.74 26.46 26.60 23.64
## [37] 26.05 24.82 29.80 34.71 23.49 24.51 26.77 31.04 33.23 24.02 25.14 27.07
## [49] 27.09 27.56 27.60 27.99 29.22 23.87 23.96 23.34 24.57 27.81 28.38 31.44
## [61] 32.02 43.47 26.77 23.03 25.14 30.25 23.14 25.02 25.15 27.03 27.12 28.99
## [73] 29.85 23.45 24.35 25.27 25.64 25.83 26.53 27.10 27.20 28.24 28.77 29.56
## [85] 31.59 36.38 38.06 23.34 27.73 30.67 25.94 31.56 23.47 25.31 31.63 32.62
## [97] 34.47 39.79 23.98 34.85 22.99 30.78 34.69 30.00 35.43 35.71 23.29 23.88
## [109] 26.11 26.96 27.05 27.55 34.27 24.17 25.13 25.89 26.55 26.69 27.90 29.26
## [121] 29.47 31.03 32.46 35.67 29.39 30.77 22.38 22.89 25.76 27.99 31.17 32.31
## [133] 36.33 30.19 35.07 23.79 34.89 36.37 31.28 27.96 23.46 25.10 25.37 27.33
## [145] 29.57 31.28 34.75 23.29 23.52 25.03 26.56 28.43 29.64 30.74 31.18 33.77
## [157] 35.66 40.97 23.54 31.29 31.37 23.15 24.90 26.27 26.79 29.07 29.47 32.55
## [169] 40.29 40.58 33.01 24.11 30.35 35.50 23.29 31.48 24.73 25.41 25.66 31.15
## [181] 31.68 31.85 32.01 34.23 26.93 27.54 28.21 29.55 29.71 29.83 33.57 34.75
## [193] 27.23 24.07 24.20 24.38 24.50 25.21 25.75 26.48 26.89 26.97 28.53 30.46
## [205] 31.15 27.05 28.68 29.95 31.45 32.03 23.47 37.21 25.78 26.63 27.31 27.94
## [217] 30.55 30.98 37.27 40.68 23.74 24.49 25.74 26.47 26.73 27.01 27.16 30.50
## [229] 37.43 39.75 39.85 25.67 30.04 34.69 24.09 27.77 28.41 28.81 30.01 31.57
## [241] 24.15 26.86 32.52 26.84 28.19 29.74 26.16 24.02 24.58 24.63 25.35 30.82
## [253] 32.89 33.33 33.52 22.81 23.23 24.96 25.29 25.93 26.07 26.09 26.81 31.84
## [265] 33.49 35.27 35.82 42.30 24.20 27.08 28.67 24.76 23.79 26.61 28.50 29.42
## [277] 36.24 23.36 23.90 25.18 28.62 36.32 33.53 25.18 25.69 26.26 27.12 27.49
## [289] 27.53 27.69 23.32 24.24 25.50 27.60 27.73 31.56 34.19 36.78 31.35 33.03
## [301] 22.39 27.99 23.10 25.02 25.38 26.14 26.52 28.06 28.11 30.21 30.80 31.21
## [313] 27.22 30.17 31.36 26.03 36.51 30.99 22.02 24.97 26.78 30.69 30.95 32.51
## [325] 32.74 33.09 22.55 24.13 24.79 25.17 25.96 26.29 29.99 30.46 32.24 27.61
## [337] 28.20 23.45 24.09 24.94 27.43 27.94 30.60 35.23 24.21 28.85 33.95 35.25
## [349] 27.12 26.68 32.66 23.34 25.96 29.98 30.00 33.09 38.28 21.78 22.31 22.64
## [361] 24.63 25.44 26.11 27.55 29.24 29.95 30.30 38.85 40.77 25.18 31.39 33.74
## [373] 24.25 27.50 31.42 24.02 24.34 24.73 24.97 29.49 29.54 42.30 29.78 28.63
## [385] 30.99 26.33 23.08 26.18 26.24 26.63 28.03 28.26 29.12 22.14 22.27 23.98
## [397] 24.65 24.66 24.76 25.55 25.57 26.03 27.05 27.85 28.70 26.97 22.85 23.19
## [409] 23.01 23.08 23.13 24.21 25.13 23.87 33.98 24.42 27.56 37.88 31.05 31.56
## [421] 27.44 28.68 30.19 30.69 38.11 24.47 24.94 26.42 27.32 28.54 28.77 29.22
## [433] 30.06 30.18 33.75 26.97 27.12 28.92 35.55 41.21 30.06 24.11 24.63 28.11
## [445] 28.62 28.90 29.50 40.53 31.51 26.65 32.95 33.61 27.50 30.90 24.67 25.22
## [457] 25.84 27.20 32.17 39.25 22.78 25.62 26.59 28.50 30.35 30.94 32.26 32.35
## [469] 33.26 23.44 29.09 36.67 22.89 29.09 23.36 25.90 26.00 26.51 28.48 29.73
## [481] 29.83 31.02 32.77 25.48 24.04 35.12 22.81 33.60 36.39 22.43 24.67 24.89
## [493] 26.17 29.54 39.49 22.70 25.60 25.74 26.72 27.23 29.86 30.29 33.90 36.13
## [505] 37.04 33.15 34.39 38.98 29.35 22.59 28.77 30.52 32.69 33.75 34.08 23.46
## [517] 26.59 23.87 32.57 35.82 28.38 35.02 25.79 24.77 27.93 29.85 30.55 31.62
## [529] 24.00 24.16 25.65 26.49 28.10 28.58 29.86 32.27 22.61 24.85 31.47 27.33
## [541] 23.26 23.35 25.38 25.45 27.85 27.97 33.77 26.67 29.31 37.43 27.12 30.48
## [553] 30.67 37.38 22.81 24.41 24.94 25.72 26.33 27.36 29.14 32.61 33.87 25.89
## [565] 26.13 29.10 29.13 29.17 31.28 31.81 38.43 24.46 34.73 24.53 27.17 36.53
## [577] 23.26 26.07 27.31 28.53 28.62 28.70 32.16 26.90 33.67 31.00 34.88 34.68
## [589] 48.52 21.90 25.70 29.06 29.85 33.48 34.30 40.66 22.53 22.86 24.07 24.19
## [601] 27.38 28.30 29.50 29.84 30.03 30.51 33.41 33.60 35.60 24.33 37.38 23.72
## [613] 37.30 23.13 25.54 25.81 27.39 27.97 33.67 35.35 37.39 40.93 24.19 29.43
## [625] 35.54 36.14 28.80 29.90 32.70 22.55 27.45 32.72 32.97 33.32 24.21 25.03
## [637] 25.85 26.36 26.51 30.16 30.88 32.57 37.25 37.68 24.36 32.82 32.68 23.76
## [649] 24.43 26.13 26.92 31.14 34.71 38.23 31.59 32.68 34.93 38.49 27.35 26.26
## [661] 27.56 24.95 28.88 30.57 31.24 33.01 23.58 23.94 24.01 26.52 27.45 27.56
## [673] 28.77 29.28 32.14 34.51 37.10 23.98 29.73 26.75 27.13 31.33 24.62 24.98
## [685] 25.25 26.04 26.22 26.45 27.77 35.16 27.09 29.23 27.54 28.10 31.49 34.07
## [697] 28.66 27.28 30.80 24.18 26.25 27.52 27.78 29.50 30.39 22.11 25.94 26.51
## [709] 27.71 27.82 28.53 28.56 30.51 31.28 34.87 39.28 27.90 28.20 28.26 23.01
## [721] 23.18 25.81 26.54 29.27 31.72 36.91 44.28 30.96 25.86 28.06 25.91 26.63
## [733] 27.32 25.34 26.36 26.36 26.86 28.29 28.44 29.45 23.49 24.62 26.09 26.41
## [745] 26.55 28.32 28.49 32.04 32.34 34.97 25.95 29.17 23.87 24.45 24.64 24.68
## [757] 25.08 25.33 25.84 27.39 28.84 29.19 29.19 26.01 28.92 25.60 24.81 26.01
## [769] 28.12 29.95 31.20 32.87 34.14 36.11 23.74 24.87 26.03 26.19 26.77 27.21
## [781] 28.59 29.42 30.57 30.84 31.41 39.38 39.79 28.79 33.77 27.36 33.85 25.75
## [793] 26.31 27.50 27.77 40.88 23.58 30.73 30.43 32.17 21.58 24.94 25.71 29.26
## [805] 30.79 33.36 34.74 22.06 22.30 23.73 25.49 25.65 27.86 29.16 29.58 29.86
## [817] 30.02 23.27 32.51 22.41 25.08 20.90 21.52 25.85 26.75 27.27 30.52 32.55
## [829] 36.03 27.90 28.06 32.61 28.08 37.34 26.22 26.67 28.91 32.73 34.75 35.72
## [841] 42.60 24.96 25.30 25.44 25.53 26.52 26.98 27.06 28.20 28.86 34.32 34.52
## [853] 24.77 35.25 39.85 21.85 22.41 24.28 26.39 28.80 32.56 32.74 31.84 35.49
## [865] 24.63 34.87 27.12 27.99 25.82 27.07 29.11 30.98 36.68 37.66 25.08 25.19
## [877] 25.73 27.92 28.89 31.01 31.58 32.34 34.48 36.88 38.31 30.21 31.14 32.11
## [889] 25.37 25.50 29.57 29.86 31.84 33.99 31.91 34.44 23.06 26.60 31.78 26.27
## [901] 29.39 21.46 22.68 25.43 25.57 34.65 23.75 26.06 26.41 26.54 26.77 27.55
## [913] 28.31 29.04 31.28 36.47 25.08 26.51 25.76 23.10 23.47 23.85 24.49 25.19
## [925] 27.48 28.88 29.14 29.77 22.52 25.03 25.25 27.30 32.08 26.60 26.89 26.66
## [937] 29.50 29.75 32.84 38.30 39.75 24.06 24.49 24.53 25.66 26.28 27.47 29.60
## [949] 30.14 31.15 35.13 35.67 24.69 22.44 30.36 23.65 23.66 24.94 24.96 26.66
## [961] 27.76 29.42 32.18 26.27 30.42 35.88 23.62 38.76 30.09 26.03 28.23 30.23
## [973] 23.92 23.96 24.08 24.94 25.23 26.21 26.42 27.14 29.44 29.56 30.95 31.17
## [985] 24.94 26.50 29.60 32.43 37.16 25.21 25.45 26.24 29.80 30.15 30.95 33.41
## [997] 27.01 30.57 22.34 30.26 26.20 28.45 25.23 25.24 26.78 27.26 28.16 28.38
## [1009] 28.48 22.73 24.63 24.95 25.24 25.35 25.37 25.43 26.43 28.42 28.53 29.13
## [1021] 29.23 30.22 32.30 27.63 25.93 26.80 28.94 25.40 25.84 26.54 27.45 29.05
## [1033] 29.08 22.42
sort(age, decreasing = TRUE)
## [1] 48.52 44.28 43.47 42.60 42.30 42.30 41.21 40.97 40.93 40.88 40.77 40.68
## [13] 40.66 40.58 40.53 40.29 39.85 39.85 39.79 39.79 39.75 39.75 39.49 39.38
## [25] 39.28 39.25 38.98 38.85 38.76 38.49 38.43 38.31 38.30 38.28 38.23 38.11
## [37] 38.06 37.88 37.74 37.68 37.66 37.43 37.43 37.39 37.38 37.38 37.36 37.34
## [49] 37.30 37.27 37.25 37.21 37.16 37.10 37.04 36.91 36.88 36.78 36.68 36.67
## [61] 36.53 36.51 36.47 36.40 36.39 36.38 36.37 36.33 36.32 36.24 36.14 36.13
## [73] 36.11 36.03 35.88 35.82 35.82 35.72 35.71 35.67 35.67 35.66 35.60 35.55
## [85] 35.54 35.50 35.49 35.43 35.35 35.27 35.25 35.25 35.23 35.16 35.13 35.12
## [97] 35.07 35.02 34.97 34.93 34.89 34.88 34.87 34.87 34.85 34.75 34.75 34.75
## [109] 34.74 34.73 34.71 34.71 34.69 34.69 34.68 34.67 34.65 34.52 34.51 34.48
## [121] 34.47 34.44 34.39 34.32 34.30 34.27 34.23 34.19 34.14 34.08 34.07 33.99
## [133] 33.98 33.95 33.90 33.87 33.85 33.77 33.77 33.77 33.77 33.75 33.75 33.74
## [145] 33.67 33.67 33.61 33.60 33.60 33.57 33.53 33.52 33.49 33.48 33.41 33.41
## [157] 33.36 33.33 33.32 33.31 33.26 33.23 33.15 33.09 33.09 33.03 33.01 33.01
## [169] 32.97 32.95 32.89 32.87 32.84 32.82 32.77 32.74 32.74 32.73 32.72 32.70
## [181] 32.69 32.68 32.68 32.66 32.62 32.61 32.61 32.57 32.57 32.56 32.55 32.55
## [193] 32.52 32.51 32.51 32.51 32.46 32.43 32.35 32.34 32.34 32.33 32.31 32.30
## [205] 32.27 32.26 32.24 32.18 32.17 32.17 32.16 32.14 32.11 32.08 32.04 32.03
## [217] 32.02 32.01 31.91 31.85 31.84 31.84 31.84 31.81 31.78 31.74 31.72 31.68
## [229] 31.63 31.62 31.61 31.59 31.59 31.59 31.58 31.57 31.56 31.56 31.56 31.51
## [241] 31.49 31.48 31.47 31.45 31.44 31.42 31.41 31.39 31.37 31.36 31.35 31.33
## [253] 31.29 31.28 31.28 31.28 31.28 31.28 31.24 31.21 31.20 31.18 31.17 31.17
## [265] 31.15 31.15 31.15 31.14 31.14 31.06 31.05 31.04 31.03 31.02 31.01 31.00
## [277] 30.99 30.99 30.98 30.98 30.96 30.95 30.95 30.95 30.94 30.90 30.89 30.88
## [289] 30.84 30.82 30.80 30.80 30.79 30.78 30.77 30.74 30.73 30.69 30.69 30.67
## [301] 30.67 30.60 30.57 30.57 30.57 30.55 30.55 30.52 30.52 30.51 30.51 30.50
## [313] 30.48 30.46 30.46 30.43 30.42 30.39 30.36 30.35 30.35 30.30 30.29 30.29
## [325] 30.26 30.25 30.23 30.22 30.21 30.21 30.19 30.19 30.18 30.17 30.16 30.15
## [337] 30.14 30.09 30.06 30.06 30.04 30.03 30.02 30.01 30.00 30.00 29.99 29.98
## [349] 29.95 29.95 29.95 29.95 29.90 29.86 29.86 29.86 29.86 29.85 29.85 29.85
## [361] 29.84 29.83 29.83 29.80 29.80 29.78 29.77 29.75 29.74 29.73 29.73 29.71
## [373] 29.64 29.60 29.60 29.58 29.57 29.57 29.56 29.56 29.55 29.54 29.54 29.50
## [385] 29.50 29.50 29.50 29.49 29.47 29.47 29.45 29.44 29.43 29.42 29.42 29.42
## [397] 29.39 29.39 29.35 29.31 29.28 29.27 29.26 29.26 29.24 29.23 29.23 29.22
## [409] 29.22 29.19 29.19 29.17 29.17 29.16 29.14 29.14 29.13 29.13 29.12 29.11
## [421] 29.10 29.10 29.09 29.09 29.08 29.07 29.06 29.05 29.04 28.99 28.94 28.92
## [433] 28.92 28.91 28.90 28.89 28.88 28.88 28.86 28.85 28.84 28.81 28.80 28.80
## [445] 28.79 28.77 28.77 28.77 28.77 28.70 28.70 28.68 28.68 28.67 28.66 28.66
## [457] 28.63 28.62 28.62 28.62 28.59 28.58 28.56 28.54 28.53 28.53 28.53 28.53
## [469] 28.50 28.50 28.49 28.48 28.48 28.45 28.44 28.43 28.42 28.41 28.38 28.38
## [481] 28.38 28.35 28.32 28.31 28.30 28.29 28.26 28.26 28.24 28.23 28.21 28.20
## [493] 28.20 28.20 28.19 28.16 28.12 28.11 28.11 28.10 28.10 28.09 28.08 28.06
## [505] 28.06 28.06 28.03 27.99 27.99 27.99 27.99 27.97 27.97 27.96 27.94 27.94
## [517] 27.93 27.92 27.90 27.90 27.90 27.86 27.85 27.85 27.82 27.81 27.78 27.77
## [529] 27.77 27.77 27.76 27.73 27.73 27.71 27.69 27.63 27.61 27.60 27.60 27.56
## [541] 27.56 27.56 27.56 27.55 27.55 27.55 27.54 27.54 27.53 27.52 27.51 27.50
## [553] 27.50 27.50 27.49 27.48 27.47 27.45 27.45 27.45 27.44 27.43 27.39 27.39
## [565] 27.38 27.36 27.36 27.35 27.33 27.33 27.32 27.32 27.31 27.31 27.30 27.28
## [577] 27.27 27.26 27.23 27.23 27.22 27.21 27.20 27.20 27.17 27.16 27.14 27.13
## [589] 27.12 27.12 27.12 27.12 27.12 27.12 27.10 27.09 27.09 27.08 27.07 27.07
## [601] 27.06 27.05 27.05 27.05 27.03 27.01 27.01 26.98 26.97 26.97 26.97 26.96
## [613] 26.93 26.92 26.90 26.89 26.89 26.86 26.86 26.84 26.81 26.80 26.79 26.78
## [625] 26.78 26.77 26.77 26.77 26.77 26.75 26.75 26.73 26.72 26.69 26.68 26.67
## [637] 26.67 26.66 26.66 26.65 26.63 26.63 26.63 26.61 26.60 26.60 26.60 26.59
## [649] 26.59 26.56 26.55 26.55 26.54 26.54 26.54 26.53 26.52 26.52 26.52 26.51
## [661] 26.51 26.51 26.51 26.50 26.49 26.48 26.47 26.46 26.45 26.43 26.42 26.42
## [673] 26.41 26.41 26.39 26.36 26.36 26.36 26.33 26.33 26.31 26.29 26.28 26.27
## [685] 26.27 26.27 26.26 26.26 26.25 26.24 26.24 26.22 26.22 26.21 26.20 26.19
## [697] 26.18 26.17 26.16 26.14 26.13 26.13 26.11 26.11 26.09 26.09 26.07 26.07
## [709] 26.06 26.05 26.04 26.03 26.03 26.03 26.03 26.01 26.01 26.00 25.96 25.96
## [721] 25.95 25.94 25.94 25.93 25.93 25.91 25.90 25.89 25.89 25.86 25.85 25.85
## [733] 25.84 25.84 25.84 25.83 25.82 25.81 25.81 25.79 25.78 25.76 25.76 25.75
## [745] 25.75 25.75 25.74 25.74 25.73 25.72 25.71 25.71 25.70 25.69 25.67 25.66
## [757] 25.66 25.65 25.65 25.64 25.62 25.60 25.60 25.57 25.57 25.55 25.54 25.53
## [769] 25.50 25.50 25.49 25.48 25.45 25.45 25.44 25.44 25.43 25.43 25.41 25.40
## [781] 25.38 25.38 25.37 25.37 25.37 25.35 25.35 25.34 25.33 25.33 25.31 25.30
## [793] 25.29 25.27 25.25 25.25 25.24 25.24 25.23 25.23 25.22 25.21 25.21 25.19
## [805] 25.19 25.18 25.18 25.18 25.17 25.15 25.14 25.14 25.14 25.13 25.13 25.11
## [817] 25.10 25.08 25.08 25.08 25.08 25.03 25.03 25.03 25.02 25.02 24.98 24.97
## [829] 24.97 24.96 24.96 24.96 24.95 24.95 24.94 24.94 24.94 24.94 24.94 24.94
## [841] 24.94 24.90 24.89 24.87 24.85 24.82 24.81 24.79 24.77 24.77 24.76 24.76
## [853] 24.73 24.73 24.69 24.68 24.67 24.67 24.66 24.65 24.64 24.63 24.63 24.63
## [865] 24.63 24.63 24.62 24.62 24.58 24.57 24.53 24.53 24.51 24.50 24.49 24.49
## [877] 24.49 24.47 24.46 24.45 24.43 24.42 24.41 24.41 24.38 24.36 24.35 24.34
## [889] 24.33 24.32 24.28 24.25 24.24 24.21 24.21 24.21 24.20 24.20 24.19 24.19
## [901] 24.18 24.17 24.16 24.15 24.14 24.13 24.11 24.11 24.09 24.09 24.08 24.07
## [913] 24.07 24.06 24.04 24.02 24.02 24.02 24.02 24.01 24.00 23.98 23.98 23.98
## [925] 23.96 23.96 23.94 23.92 23.92 23.90 23.88 23.87 23.87 23.87 23.87 23.85
## [937] 23.79 23.79 23.76 23.75 23.74 23.74 23.73 23.72 23.70 23.66 23.65 23.64
## [949] 23.64 23.62 23.58 23.58 23.54 23.52 23.49 23.49 23.47 23.47 23.47 23.46
## [961] 23.46 23.45 23.45 23.44 23.36 23.36 23.35 23.34 23.34 23.34 23.32 23.29
## [973] 23.29 23.29 23.27 23.26 23.26 23.23 23.19 23.18 23.15 23.14 23.13 23.13
## [985] 23.13 23.10 23.10 23.08 23.08 23.06 23.03 23.01 23.01 22.99 22.89 22.89
## [997] 22.86 22.85 22.81 22.81 22.81 22.78 22.73 22.70 22.68 22.64 22.61 22.59
## [1009] 22.55 22.55 22.53 22.52 22.44 22.43 22.42 22.41 22.41 22.39 22.38 22.34
## [1021] 22.31 22.30 22.27 22.14 22.11 22.06 22.02 21.90 21.85 21.78 21.58 21.52
## [1033] 21.46 20.90
This does not tell us the name of the old players! We need to order the rows by age. Order gives the order of the rows according to the sorted vector.
o <- order(age, decreasing = TRUE)
o
## [1] 589 727 62 841 268 382 440 158 622 796 369 220 596 170
## [15] 448 169 231 855 98 787 230 941 495 786 716 460 508 368
## [29] 968 658 571 885 940 357 654 425 87 418 33 644 874 229
## [43] 550 621 554 611 24 834 613 219 643 212 989 677 505 726
## [57] 884 298 873 472 576 317 916 23 489 86 138 133 282 277
## [71] 626 504 774 829 966 267 520 840 106 124 952 157 609 439
## [85] 625 174 864 105 620 266 348 854 344 690 951 486 135 522
## [99] 750 657 137 587 715 866 100 147 192 839 807 573 40 653
## [113] 103 234 588 16 906 852 676 883 97 896 507 851 595 113
## [127] 184 297 773 515 696 894 415 347 503 563 791 32 156 547
## [141] 789 435 514 372 585 619 452 488 608 191 283 255 265 594
## [155] 607 996 806 254 634 22 469 45 506 326 356 300 171 666
## [169] 633 451 253 772 939 646 483 325 862 838 632 629 513 647
## [183] 656 351 96 562 832 519 642 861 168 828 243 15 324 819
## [197] 123 988 468 749 882 26 132 1023 536 467 335 963 459 800
## [211] 583 675 888 933 748 210 61 183 895 182 264 863 893 570
## [225] 899 3 725 181 95 528 21 8 85 655 881 240 92 296
## [239] 420 449 695 176 539 209 60 375 785 371 161 315 299 682
## [253] 160 139 146 569 714 915 665 312 771 155 131 984 180 205
## [267] 950 652 887 14 419 44 122 482 880 586 318 385 218 872
## [281] 728 323 983 995 466 454 31 641 784 252 311 699 805 102
## [295] 126 154 798 322 424 90 553 343 664 783 998 217 527 512
## [309] 827 606 713 228 552 204 334 799 965 705 955 173 465 367
## [323] 20 502 1000 66 972 1022 310 886 134 423 434 314 640 994
## [337] 949 969 433 441 233 605 817 239 104 355 333 354 7 208
## [351] 366 770 628 501 535 816 892 73 526 593 604 190 481 39
## [365] 993 383 928 938 246 480 679 189 153 948 987 815 145 891
## [379] 84 982 188 381 494 447 603 704 937 380 121 167 740 981
## [393] 624 276 782 962 125 901 509 549 674 724 120 804 365 692
## [407] 1021 53 432 762 763 568 752 814 561 927 567 1020 393 871
## [421] 13 566 471 474 1033 166 592 1032 914 72 1027 438 765 837
## [435] 446 879 663 926 850 346 761 238 627 860 788 83 431 511
## [449] 673 405 582 207 422 271 12 697 384 281 445 581 781 534
## [463] 712 430 203 580 711 1019 275 464 747 479 1009 1002 739 152
## [477] 1018 237 59 521 1008 30 746 913 602 738 392 719 82 971
## [491] 187 337 718 849 245 1007 769 309 444 533 694 19 833 308
## [505] 730 831 391 52 130 302 868 546 618 140 216 342 525 878
## [519] 119 717 830 813 404 545 710 58 703 236 689 795 961 89
## [533] 295 709 290 1024 336 51 294 50 417 661 672 112 364 912
## [547] 186 693 289 702 11 374 453 794 288 925 947 631 671 1031
## [561] 421 341 617 760 601 560 790 659 144 540 429 733 215 579
## [575] 932 698 826 1006 193 500 313 780 81 458 575 227 980 681
## [589] 71 287 349 437 551 867 80 49 691 270 48 870 848 111
## [603] 206 403 70 226 997 847 202 406 436 110 185 651 584 201
## [617] 935 242 737 244 263 1026 165 321 1005 43 63 779 911 680
## [631] 825 225 499 118 350 548 836 936 960 450 214 390 732 274
## [645] 35 898 934 463 517 151 117 745 723 910 1030 79 307 670
## [659] 846 478 639 708 918 986 532 200 224 34 688 1017 428 979
## [673] 744 909 859 638 735 736 386 559 793 332 946 164 900 964
## [687] 286 660 701 389 992 687 835 978 1001 778 388 493 247 306
## [701] 565 650 109 363 262 743 261 578 908 37 686 316 402 777
## [715] 970 764 768 477 331 353 751 91 707 260 1025 731 476 116
## [729] 564 729 637 824 457 759 1029 78 869 616 722 523 213 129
## [743] 919 10 199 792 223 498 877 558 29 803 591 285 232 179
## [757] 945 531 812 77 462 497 766 401 905 400 615 845 293 890
## [771] 811 484 544 991 362 844 904 1016 178 1028 305 543 143 889
## [785] 1015 251 1014 734 2 758 94 843 259 76 685 931 1004 1013
## [799] 977 1003 456 198 990 876 924 280 284 370 330 69 18 47
## [813] 65 115 413 9 142 757 821 875 917 150 636 930 68 304
## [827] 684 320 379 258 842 959 662 1012 340 427 557 802 958 976
## [841] 985 163 492 776 538 38 767 329 524 853 272 399 177 378
## [855] 953 756 455 491 398 397 755 250 361 443 865 1011 683 742
## [869] 249 57 574 944 42 197 222 923 943 426 572 754 649 416
## [883] 28 556 196 645 75 377 610 17 858 373 292 345 412 635
## [897] 195 269 600 623 700 114 530 241 27 328 172 442 235 339
## [911] 975 194 599 942 485 6 46 248 376 669 529 99 396 678
## [925] 55 974 668 1 973 279 108 54 414 518 753 922 136 273
## [939] 648 907 221 775 810 612 5 957 956 4 36 967 667 797
## [953] 159 149 41 741 93 211 921 141 516 74 338 470 278 475
## [967] 542 56 88 352 291 107 148 175 818 541 577 257 408 721
## [981] 162 67 25 411 614 303 920 387 410 897 64 409 720 101
## [995] 128 473 598 407 256 487 555 461 1010 496 903 360 537 510
## [1009] 327 630 597 929 954 490 1034 820 857 301 127 999 359 809
## [1023] 395 394 706 808 319 590 856 358 801 823 902 822
Now we need the first three row indices. We can subset or use
head()
which is the same as limit in SQL.
o[1:3]
## [1] 589 727 62
head(o, n = 3)
## [1] 589 727 62
Subset the selected rows.
df[o[1:3],]
## First.Name Last.Name Team Position Height.inches. Weight.pounds.
## 589 Julio Franco NYM First Baseman 73 188
## 727 Jamie Moyer PHI Starting Pitcher 72 175
## 62 Randy Johnson ARZ Starting Pitcher 82 231
## Age
## 589 48.52
## 727 44.28
## 62 43.47
Putting it all together into a single line of code
df[head(order(df$Age, decreasing = TRUE), n = 3) ,]
## First.Name Last.Name Team Position Height.inches. Weight.pounds.
## 589 Julio Franco NYM First Baseman 73 188
## 727 Jamie Moyer PHI Starting Pitcher 72 175
## 62 Randy Johnson ARZ Starting Pitcher 82 231
## Age
## 589 48.52
## 727 44.28
## 62 43.47
See: dplyr Data Transformation Cheatsheet (tidyverse)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Note: With tidyverse you can read the data using:
df <- read_csv(file = "MLB_cleaned.csv", col_types = "ccffddd")
Arrange the rows in descending order of Age and then limit the output
to 3 with head()
.
df %>% arrange(desc(Age)) %>% head(n = 3)
## First.Name Last.Name Team Position Height.inches. Weight.pounds.
## 1 Julio Franco NYM First Baseman 73 188
## 2 Jamie Moyer PHI Starting Pitcher 72 175
## 3 Randy Johnson ARZ Starting Pitcher 82 231
## Age
## 1 48.52
## 2 44.28
## 3 43.47