|
31 | 31 | <article> |
32 | 32 | <h2>R mode</h2> |
33 | 33 | <form><textarea id="code" name="code"> |
34 | | -# Code from http://www.mayin.org/ajayshah/KB/R/ |
| 34 | +X <- list(height = 5.4, weight = 54) |
| 35 | +cat("Printing objects: "); print(X) |
| 36 | +print("Accessing individual elements:") |
| 37 | +cat(sprintf("Your height is %s and your weight is %s\n", X$height, X$weight)) |
35 | 38 |
|
36 | | -# FIRST LEARN ABOUT LISTS -- |
37 | | -X = list(height=5.4, weight=54) |
38 | | -print("Use default printing --") |
39 | | -print(X) |
40 | | -print("Accessing individual elements --") |
41 | | -cat("Your height is ", X$height, " and your weight is ", X$weight, "\n") |
42 | | - |
43 | | -# FUNCTIONS -- |
| 39 | +# Functions: |
44 | 40 | square <- function(x) { |
45 | | - return(x*x) |
| 41 | + return(x * x) |
46 | 42 | } |
47 | | -cat("The square of 3 is ", square(3), "\n") |
| 43 | +cat(sprintf("The square of 3 is %s\n", square(3))) |
48 | 44 |
|
49 | | - # default value of the arg is set to 5. |
50 | | -cube <- function(x=5) { |
51 | | - return(x*x*x); |
| 45 | +# In R, the last expression in a function is, by default, what is |
| 46 | +# returned. The idiomatic way to write the function is: |
| 47 | +square <- function(x) { |
| 48 | + x * x |
52 | 49 | } |
53 | | -cat("Calling cube with 2 : ", cube(2), "\n") # will give 2^3 |
54 | | -cat("Calling cube : ", cube(), "\n") # will default to 5^3. |
| 50 | +# or, for functions with short content: |
| 51 | +square <- function(x) x * x |
55 | 52 |
|
56 | | -# LEARN ABOUT FUNCTIONS THAT RETURN MULTIPLE OBJECTS -- |
57 | | -powers <- function(x) { |
58 | | - parcel = list(x2=x*x, x3=x*x*x, x4=x*x*x*x); |
59 | | - return(parcel); |
60 | | -} |
| 53 | +# Function arguments with default values: |
| 54 | +cube <- function(x = 5) x * x * x |
| 55 | +cat(sprintf("Calling cube with 2 : %s\n", cube(2)) # will give 2^3 |
| 56 | +cat(sprintf("Calling cube : %s\n", cube()) # will default to 5^3. |
61 | 57 |
|
62 | | -X = powers(3); |
63 | | -print("Showing powers of 3 --"); print(X); |
| 58 | +powers <- function(x) list(x2 = x*x, x3 = x*x*x, x4 = x*x*x*x) |
64 | 59 |
|
65 | | -# WRITING THIS COMPACTLY (4 lines instead of 7) |
| 60 | +cat("Powers of 3: "); print(powers(3)) |
66 | 61 |
|
67 | | -powerful <- function(x) { |
68 | | - return(list(x2=x*x, x3=x*x*x, x4=x*x*x*x)); |
69 | | -} |
70 | | -print("Showing powers of 3 --"); print(powerful(3)); |
| 62 | +# Data frames |
| 63 | +df <- data.frame(letters = letters[1:5], '#letter' = 1:5) |
| 64 | +print(df$letters) |
| 65 | +print(df$`#letter`) |
71 | 66 |
|
72 | | -# In R, the last expression in a function is, by default, what is |
73 | | -# returned. So you could equally just say: |
74 | | -powerful <- function(x) {list(x2=x*x, x3=x*x*x, x4=x*x*x*x)} |
| 67 | +# Operators: |
| 68 | +m1 <- matrix(1:6, 2, 3) |
| 69 | +m2 <- m1 %*% t(m1) |
| 70 | +cat("Matrix product: "); print(m2) |
| 71 | + |
| 72 | +# Assignments: |
| 73 | +a <- 1 |
| 74 | +b <<- 2 |
| 75 | +c = 3 |
| 76 | +4 -> d |
| 77 | +5 ->> e |
75 | 78 | </textarea></form> |
76 | 79 | <script> |
77 | 80 | var editor = CodeMirror.fromTextArea(document.getElementById("code"), {}); |
|
0 commit comments