In this one tutorial I will cover the basic syntax of the R programming language as well as provide numerous examples on plotting and statistical analysis. R is widely considered to be the best language for statistical analysis and data mining. R makes it extremely easy to perform numerous complex calculations with ease and its plotting system is second to none.
All of the heavily commented code follows the video below. Here is the baseball data.
If you like videos like this consider donating $1, or simply turn off Ad Blocker. Either helps a lot!
[googleplusone]
Code from the Video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# This is a comment # Comment multiple lines in RStudio by selecting # CMD + SHIFT + C (Mac) / CTRL + SHIFT + C Windows # getwd() returns the working directory # setwd("working/directory") sets the working directory # You can highlight code and execute just it in the console # ----- FUN STARTING EXAMPLE ----- # ----- SCATTERPLOTS ----- # Use a scatterplot to see if batting average is directly # connected to runs produced # Load player data mlbPlayers = read.table(file=file.choose(), header=T, sep=" ", na.strings="`", stringsAsFactors=F) # Grab just RBIs and Avg for each player # playerData is known as a data frame (Table of Data) # We get the stats we want by passing that list in a vector playerData = mlbPlayers[,c("RBI","AVG")] # Create the file png(file="player_rbi_avg.png") # Create the plot plot(x=playerData$RBI, y=playerData$AVG, xlab="RBI", ylab="AVG", main="RBIs and Average") # Create the file dev.off() # ----- ASSIGNMENT ----- # You can assign a value using = or <- myNum = 5 myNum # ----- VARIABLES ----- # Variable names start with a letter and can contain # numbers, underscores and dots # Most languages use data types to define how much # space to set asside in memory # Variables in R are assigned R Objects # Types are dynamic which means a variable names data # type changes based on the data assigned to it # Here are the Vector types # numeric print(class(4)) # integer print(class(4L)) # logical (TRUE, FALSE, T, F) print(class(TRUE)) # complex print(class(1 + 4i)) # character print(class("Sample")) # raw when converted into raw bytes print(class(charToRaw("Sample"))) # You can check an objects class with # is.integer(), is.numeric(), is.matrix(), is.data.frame(), # is.logical(), is.vector(), is.character() # You can convert to different classes with # as.integer(), as.numeric(),... # ----- ARITHMETIC OPERATORS ----- sprintf("4 + 5 = %d", 4 + 5) sprintf("4 - 5 = %d", 4 - 5) sprintf("4 * 5 = %d", 4 * 5) sprintf("4 / 5 = %1.3f", 4 / 5) # Modulus or remainder of division sprintf("5 %% 4 = %d", 5 %% 4) # Value raised to the exponent of the next sprintf("4^2 = %d", 4^2) # ----- VECTORS ----- # Vectors store multiple values # Create a vector numbers = c(3, 2, 0, 1, 8) numbers # Get value by index numbers[1] # Get the number of items length(numbers) # Get the last value numbers[length(numbers)] # Get everything but an index numbers[-1] # Get the 1st 2 values numbers[c(1,2)] # Get the 2nd and 3rd numbers[2:3] # Replace a value numbers[5] = 1 numbers # Replace the 4th and 5th with 2 numbers[c(4,5)] = 2 numbers # sort values (decreasing can be TRUE or FALSE) sort(numbers, decreasing=TRUE) # Generate a sequence from 1 to 10 oneToTen = 1:10 oneToTen # Sequence from 3 to 27 adding 3 each time add3 = seq(from=3, to=27, by=3) add3 # Create 10 evens from 2 evens = seq(from=2, by=2, length.out=10) evens # Find out if a value is in vector sprintf("4 in evens %s", 4 %in% evens) # rep() repeats a value/s x, a number of times and # each defines how many times to repeat each item rep(x=2, times=5, each=2) rep(x=c(1,2,3), times=2, each=2) # ----- RELATIONAL OPERATORS ----- iAmTrue = TRUE iAmFalse = FALSE sprintf("4 == 5 : %s", 4 == 5) sprintf("4 != 5 : %s", 4 != 5) sprintf("4 > 5 : %s", 4 > 5) sprintf("4 < 5 : %s", 4 < 5) sprintf("4 >= 5 : %s", 4 >= 5) sprintf("4 <= 5 : %s", 4 <= 5) # Relational operator vector tricks oneTo20 = c(1:20) # Create vector of Ts and Fs depending on condition isEven = oneTo20 %% 2 == 0 isEven # Create array of evens justEvens = oneTo20[oneTo20 %% 2 == 0] justEvens # ----- LOGICAL OPERATORS ----- cat("TRUE && FALSE = ", T && F, "\n") cat("TRUE || FALSE = ", T || F, "\n") cat("!TRUE = ", !T, "\n") # ----- DECISION MAKING ----- age = 18 # if, else and else if works like other languages if(age >= 18) { print("Drive and Vote") } else if (age >= 16){ print("Drive") } else { print("Wait") } # ----- SWITCH ----- # Used when you have a limited set of possible values grade = "Z" switch(grade, "A" = print("Great"), "B" = print("Good"), "C" = print("Ok"), "D" = print("Bad"), "F" = print("Terrible"), print("No Such Grade")) # ----- STRINGS ----- str1 = "This is a string" # String length nchar(string1) # You can compare strings where later letters are considered # greater than sprintf("Dog > Egg : %s", "Dog" > "Egg") sprintf("Dog == Egg : %s", "Dog" == "Egg") # Combine strings and define sperator if any str2 = paste("Owl", "Bear", sep="") str2 # Remove bear from the string substr(x=str2, start=4, stop=7) # Substitute one string with another sub(pattern="Owl", replacement="Hawk", x=str2) # Substitute all matches gsub(pattern="Egg", replacement="Chicken", x="Egg Egg") # Split string into vector strVect = strsplit("A dog ran fast", " ") strVect # ----- FACTORS ------ # Factors are used when you have a limited number of values # that are strings or integers # Create a factor vector direction = c("Up", "Down", "Left", "Right", "Left", "Up") factorDir = factor(direction) # Check if it's a Factor is.factor(factorDir) # A Factor object contains levels which store all possible # values levels(x=factorDir) # You can define your levels and their orders dow = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") wDays = c("Tuesday", "Thursday", "Monday") wdFact = factor(x=wDays, levels=dow, ordered=T) wdFact # ----- DATA FRAMES ----- # A Data Frame is a table which contains any type # of data and an equal amount of data in each column # Each row is called a record and each column a varaible # Create customer data frame custData = data.frame(name=c("Tom", "Sally", "Sue"), age=c(43, 28, 35), stringsAsFactors=F) custData # Get data in row 1 column 1 custData[1,1] # Get all data in 1st row custData[1,1:2] # Get all ages custData[1:3, 2] # Get dimensions dim(custData) # Add another record recordMark = data.frame(name="Mark", age=33) custData = rbind(custData, recordMark) custData # Add a column representing debt debt = c(0, 25.50, 36, 48.19) custData = cbind(custData, debt) custData # Check if money is owed owesMoney = custData[custData$debt > 0,] owesMoney # ----- LOOPING ----- # Repeat until a condition is met num = 1 repeat{ print(num) num = num + 1 if(num > 5){ # Jumps out of loop break } } # Repeat while condition is true while(num > 0){ num = num - 1 # next skips the rest of the loop and jumps # back to the top if(num %% 2 == 0){ next } print(num) } # For can be used to cycle through a vector # or do the same thing a specific number of times oneTo5 = 1:5 for (i in oneTo5){ print(i) } # ----- MATRICES ----- # A Matrix stores values in rows and columns # Create a Matrix with a single column matrix1 = matrix(data=c(1,2,3,4)) matrix1 # Create a matrix with defined rows and columns matrix2 = matrix(data=c(1,2,3,4), nrow=2, ncol=2) matrix2 # You can also fill by row (You can use T or TRUE) matrix3 = matrix(data=c(1,2,3,4), nrow=2, ncol=2, byrow=T) matrix3 # Get a Matrix dimension dim(matrix3) # A value at row, column matrix3[1,2] # Get a whole row matrix3[1,] # Get a whole column matrix3[,2] # Combine vectors to make a Matrix matrix4 = rbind(1:3, 4:6, 7:9) matrix4 # Get 2nd and 3rd row matrix4[2:3,] # Get 2nd and 3rd row by ommitting the 1st matrix4[-1,] # Change the first value matrix4[1,1] = 0 matrix4 # Change the 1st row matrix4[1,] = c(10,11,12) matrix4 # ----- MULTI-DIMENSIONAL ARRAYS ----- # You can also create Matrices in layers # Create a MDA with 2 rows, columns and layers array1 = array(data=1:8, dim=c(2,2,2)) array1 # Get a value array1[1,2,2] # Experiment grabbing values like we did with the Matrix # Everything is the same # ----- FUNCTIONS ----- # A function is R is an object that performs operations # on passed attributes and then returns results # or simply control back getSum = function(num1, num2){ return(num1 + num2) } sprintf("5 + 6 = %d", getSum(5,6)) # If there is no return the last expression is returned # You can define default attribute values getDifference = function(num1=1, num2=1){ num1 - num2 } sprintf("5 - 6 = %d", getDifference(5,6)) # Return multiple values in a list makeList = function(theString){ return (strsplit(theString, " ")) } makeList("Random Words") # Handling missing arguments missFunc = function(x){ if(missing(x)){ return("Missing Argument") } else { return(x) } } missFunc() # Excepting variable number of arguments with ellipses getSumMore = function(...){ numList = list(...) sum = 0 for(i in numList){ sum = sum + i } sum } getSumMore(1,2,3,4) # Disposable / Anonymous Functions are great for # quick operations like doubling everything in a list numList = 1:10 dblList = (function(x) x * 2)(numList) dblList # Closures are functions created by functions # Create a function that finds x to a user defined # power power = function(exp){ function(x){ x ^ exp } } cubed = power(3) cubed(2) cubed(1:5) # You can store functions in lists addFunc = list( add2 = function(x) x + 2, add3 = function(x) x + 3 ) addFunc$add2(5) # ----- EXCEPTION HANDLING ----- # Used to gracefully handle errors # I handle a division with string error divide = function(num1, num2){ tryCatch( num1 / num2, error = function(e) { if(is.character(num1) || is.character(num2)){ print("Can't Divide with Strings") } }) } divide(10,"5") # ----- READING WRITING FILES ----- # Create a text file with headers fname lname sex # and the data in a txt file Use `for missing values # Save in the same directory as your R file # Supply the file to read, whether the 1st line is # headers, what seperates the data, what is being used # for missing data and false because you don't want to # convert string vectors to factors # myPeople is a data frame myPeople = read.table(file=file.choose(), header=T, sep=" ", na.strings="`", stringsAsFactors=F) myPeople # Add another person donnaRecord = data.frame(fname="Donna", lname="Heyward", sex="female") myPeople = rbind(myPeople, donnaRecord) # Update a record myPeople[7,2] = "Smith" # Update the file by supplying the data.frame, # the file to write, seperator, na, whether to # quote strings, whether to include row numbers write.table(x=myPeople, file=file.choose(), sep=" ", na="`", quote=F, row.names=F) # Get 1st 3 records head(myPeople, 3) # Get remaining records tail(myPeople) # ----- BASIC PLOTTING ----- # R provides great plotting tools # Plotting x y coordinates from a matrix # 1st 5 are x and 2nd 5 are y xy1 = matrix(data=c(1,2,3,4,5, 1,2,3,4,5), nrow=5, ncol=5) plot(xy1) # Draw a line x2 = c(1,2,3,4,5) y2 = c(1,2,3,4,5) plot(x2, y2, type="l") # Points and lines plot(x2, y2, type="b") # Points and lines with no space around points, # labels, a blue line (Find more with colors()) plot(x2, y2, type="o", main="My Plot", xlab="x axis", ylab="y axis", col="steelblue") # pch (1-25) defines different points # lty (1-6) defines different lines # xlim defines the max and min x plotting region # ylim defines the max and min y plotting region plot(x2, y2, type="b", pch=2, lty=2, xlim=c(-8,8), ylim=c(-8,8)) # Multiple plots plot(x2, y2, type="b") # Adds straight lines at 2 and 4 coordinates abline(h=c(2,4), col="red",lty=2) # Draw a 2 segmented lines with starting and ending x # and y points segments(x0=c(2,4), y0=c(2,2), x1=c(2,4), y1=c(4,4), col="red",lty=2) # Draw an arrow arrows(x0=1.5, y0=4.55, x1=2.7, y1=3.3, col="blue") # Print Text text(x=1.25, y=4.75, labels="Center") # Load a built in data.frame plot(faithful) # Highlight eruptions with a waiting time greater # then 4 eruptions4 = with(faithful, faithful[eruptions > 4,]) # Draw specific points points(eruptions4, col="red", pch=3) # ----- MATH FUNCTIONS ----- sqrt(x=100) # Get the power you raise the base to get x log(x=4, base=2) # Euler's number 2.718 to the power of x exp(x=2) # Sum all vector values sum(c(1,2,3)) # Find the mean (average) randD1 = c(1,5,6,7,10,16) mean(randD1) # The median (Middle Number or avg of middle 2) median(randD1) # Minimum value min(randD1) # Maximum value max(randD1) # Min and max range(randD1) # Rounding ceiling(4.5) floor(4.5) # Cumulatives cumsum(c(1,2,3)) cumprod(c(1,2,3)) cummax(c(7:9, 4:6, 1:3)) cummin(c(4:6, 1:3, 7:9)) # Generating Random samples # Flipping a coin 10 times and weigh the probability # of the next flip based on the previous sample(0:1,10,replace=T) sample(1:20,10,replace=T) # ----- PIE CHARTS ----- # List percentages foodPref = c(15, 35, 10, 25, 15) # Labels associated with percentages foodLabels = c("Spaghetti", "Pizza", "Mac n' Cheese", "Chicken Nuggets", "Tacos") # Where to save the image png(file="child_food_pref.png") # Colors used for each option colors = rainbow(length(foodPref)) # Create the chart pie(foodPref, foodLabels, main="Food Prefs", col=colors) # Print legend and cex shrinks the size legend("topright", c("Spaghetti", "Pizza", "Mac n' Cheese", "Chicken", "Tacos"), cex=0.8, fill=colors) # Save the chart dev.off() # 3D Pie Chart # Download package in console install.packages("plotrix") # Get the library library(plotrix) # Name the chart file png(file="3d_child_food_pref.png") # Create the chart pie3D(foodPref, labels=foodLabels, explode=0.1, start=pi/2, main="Food Prefs", labelcex=0.8) # Save the chart dev.off() # ----- BAR CHARTS ----- # Define the bar chart file png(file="food_pref_bar_chart.png") # Plot the chart barplot(foodPref, names.arg=foodLabels, xlab="Votes", ylab="Food Options", col=colors, main="Food Prefs") # Save File dev.off() # ----- REGRESSION ANALYSIS ----- # Used to study a relationship between 2 separate # pieces of data (What is the relation between batting # average and RBIS) # Create relationship model between AVG and RBIs relation = lm(playerData$RBI~playerData$AVG) # Create file png(file="RBI_AVG_Regression.png") # Plot the chart plot(playerData$AVG, playerData$RBI, main="AVG & RBI Regression", abline(lm(playerData$RBI~playerData$AVG)), xlab="AVG", ylab="RBIs") # Save chart dev.off() # ----- MULTIPLE REGRESSION ----- # Used to study the impact on one variable from numerous # others # Estimate RBIs based on other player stats playerData2 = mlbPlayers[,c("RBI","AVG","HR","OBP", "SLG","OPS")] # Create the relationship model relation2 = lm(playerData2$RBI ~ playerData2$AVG + playerData2$HR + playerData2$OBP + playerData2$SLG + playerData2$OPS) sprintf("Intercept : %f1.4", coef(relation2)[1]) # How stats effect RBIs sprintf("AVG : %f1.4", coef(relation2)[2]) sprintf("HR : %f1.4", coef(relation2)[3]) sprintf("OBP : %f1.4", coef(relation2)[4]) sprintf("SLG : %f1.4", coef(relation2)[5]) sprintf("OPS : %f1.4", coef(relation2)[6]) # Calculate expected RBIs based on stats # Evan Longoria # RBIs AVG HR OBP SLG OPS # 86 .261 20 .313 .424 .737 RBIGuess = -5.05 + (372.96 * .261) + (2.56 * 20) + (-5.41 * .313) + (-167.37 * .424) RBIGuess |
Leave a Reply