The if condition statement in R
if (condition){
do something
}
else{
do something else
}
February 29, known as a leap day in the calendar, is a date that occurs in most years that are evenly divisible by $4$, such as $2004$, $2008$, $2012$ and $2016$. Years that are evenly divisible by $100$ do not contain a leap day, with the exception of years that are evenly divisible by $400$, which do contain a leap day; thus $1900$ did not contain a leap day while $2000$ did.
Write a function called is.leapday
to check if a given year has February 29 [Hint: you may need ?%%
.].
Test your function for some years.
What can you do to improve for the function in terms of error tolerance?
If I want to check which year has a leap day for a sequence of given years. Modify your function to implement it.
B = matrix(1:10,2,5)
C = matrix(100:109,2,5)
A = matrix(NA,2,5)
for(i in 1:length(A))
{
A[i] = B[i] + C[i]
}
A
101 | 105 | 109 | 113 | 117 |
103 | 107 | 111 | 115 | 119 |
while
loopi = 0
while(i < length(A)){
i = i + 1
A[i] = B[i] + C[i]
}
A
101 | 105 | 109 | 113 | 117 |
103 | 107 | 111 | 115 | 119 |
apply()
type loops¶Calculate row sums for a matrix with a loop.
Apply sum()
function to each row of the matrix.
apply()
to an array with higher dimension.
Apply your own function to each row of the matrix.
lapply()
Apply a function to a list
mapply()
Apply a function to multiple list or vector arguments.
The ... arguments in a function.
Supply more arguments to apply()
type functions.
mat = matrix(1:100,20,5)
mat
1 | 21 | 41 | 61 | 81 |
2 | 22 | 42 | 62 | 82 |
3 | 23 | 43 | 63 | 83 |
4 | 24 | 44 | 64 | 84 |
5 | 25 | 45 | 65 | 85 |
6 | 26 | 46 | 66 | 86 |
7 | 27 | 47 | 67 | 87 |
8 | 28 | 48 | 68 | 88 |
9 | 29 | 49 | 69 | 89 |
10 | 30 | 50 | 70 | 90 |
11 | 31 | 51 | 71 | 91 |
12 | 32 | 52 | 72 | 92 |
13 | 33 | 53 | 73 | 93 |
14 | 34 | 54 | 74 | 94 |
15 | 35 | 55 | 75 | 95 |
16 | 36 | 56 | 76 | 96 |
17 | 37 | 57 | 77 | 97 |
18 | 38 | 58 | 78 | 98 |
19 | 39 | 59 | 79 | 99 |
20 | 40 | 60 | 80 | 100 |
apply(mat, 2, mean)
apply(mat, 1, mean)
apply(mat, 2, sd)
arr = array(1:240, c(20,3,4))
apply(arr, 3, mean)
apply(arr, c(1, 2), mean)
91 | 111 | 131 |
92 | 112 | 132 |
93 | 113 | 133 |
94 | 114 | 134 |
95 | 115 | 135 |
96 | 116 | 136 |
97 | 117 | 137 |
98 | 118 | 138 |
99 | 119 | 139 |
100 | 120 | 140 |
101 | 121 | 141 |
102 | 122 | 142 |
103 | 123 | 143 |
104 | 124 | 144 |
105 | 125 | 145 |
106 | 126 | 146 |
107 | 127 | 147 |
108 | 128 | 148 |
109 | 129 | 149 |
110 | 130 | 150 |
apply(mat, 2, function (x) max(x)-min(x))
maxmin = function (x) {
max(x)-min(x)
}
apply(mat, 2, maxmin)
The advantages of ()apply
.
Easy construct
Less coding
apply()
type loops is essentially a more efficient version loop in R.
Avoid loops as much as possible. We should always try to vectorize the calculations.
Use ()apply
type loop if possible.
Think a lot about under- and over-flow
Allocate the memory space before looping. This is a much slower loop.
B = matrix(1:10,2,5)
C = matrix(100:109,2,5)
A = NULL
for(i in 1:n)
{
A[i] = B[i] + C[i]
}
Apply a function to the elements of a list
lapply(X, FUN, ...)
rapply(object, f, how = c("unlist","replace", "list"), ...)
Operators with many lists
mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)
mapply("+", list1, list2, list3, SIMPLIFY = FALSE)
mapply(function(x, y) abs(x)*log(abs(y)), list1, list2, SIMPLIFY = FALSE)
lst = list (a= rnorm(10), b= 1:9, c=runif(20))
lst
length(lst)
lapply(lst, length)
lapply(lst, mean)
lst2 = list (a = lst, b = lapply(lst, mean))
lst2
rapply(lst2, length)
rapply(lst2, length, how="list")
lst1 = list (a = 4:6, b = 5:7)
lst2 = list (a = 3:5, b = 8:10)
lst1 + lst2
Error in lst1 + lst2: non-numeric argument to binary operator Traceback:
mapply("+", lst1, lst2)
a | b |
---|---|
7 | 13 |
9 | 15 |
11 | 17 |
mapply(sum, lst1, lst2)
Create a function object
myFun = function (par)
{
out = max(par1) - min(par2)
return(out)
}
Load the function:
source()
function.Execute your function.
maxmin = function (x){
out = max(x)-min(x)
return(out)
}
maxmin(1:100)
rm(list=ls())
source("code/myfun.R")
maxmin(1:100)
return()
Write a function mySummary
where the input argument is x
can be any vector and the output should contain the basic summary (mean, variance, length, max and minimum values, type) of the vector you have supplied to the function.
Test your function with some vectors (that you make up by yourself).
What will happen if your input is not a vector (e.g. a data frame weekPlanNew
) in our previous example?
The roots for the quadratic equation $ax^2+bx+c=0$ are of the form $$\label{eq:1}
x_1=\frac{-b + \sqrt {b^2-4ac}}{2a} \quad \text{and} \quad
x_2=\frac{-b - \sqrt {b^2-4ac}}{2a}$$
Write a function named quaroot
to solve the roots of given
quadratic equation with a ,b , c,
as input arguments. [Hint: you
may need the sqrt()
function]
Test your function on the following equations $$\label{eq:2}
\begin{split}
x^2+4x-1=0\\
-2x^2+2x=0\\
3x^2-9x+1=0\\
x^2 -4 = 0\\
\end{split}$$
Test your function with the equation $5x^2+2x+1=0$. What are the results? Why? [Hint: check $b^2-4ac$]?
Modify your function and return NA
if $b^2-4ac < 0$.
quaroot <- function(a, b, c)
{
x1 <- (-b+sqrt(b^2-4*a*c))/(2*a)
x2 <- (-b-sqrt(b^2-4*a*c))/(2*a)
out <- c(x1, x2)
return(out)
}
quarootm <- function(a, b, c)
{
d <- b^2-4*a*c
if(d<0)
{
x1 <- NA
x2 <- NA
}
else
{
x1 <- (-b+sqrt(d))/(2*a)
x2 <- (-b-sqrt(d))/(2*a)
}
out <- c(x1, x2)
return(out)
}
Load an R package: library("PackageName")
or
require("PackageName")
Install an R package from the Internet (CRAN):
install.packages("PackageName")
Install an R package from GitHub with the devtools
package:
install.packages("devtools")
devtools::install_github("ykang/gratis")
If you have Windows system and sometimes you R package needs to compile, you need the "Rtools" software for building packages for R under Microsoft Windows, available at https://cran.r-project.org/bin/windows/Rtools/.