xx <- list(name="Alice", gender="F", age=11)How do you access the items in a list by index?
> xx <- list(name="Alice", gender="M", age=11) > xx $name [1] "Alice" $gender [1] "M" $age [1] 11 > print(xx[[1]]) [1] "Alice" > print(xx$name) [1] "Alice"
num letter 1 1 A num letter 1 2 B ... num letter 1 26 ZAns: Here is the script (improved from the one given in class):
biglist <- NULL for(i in 1:26) { biglist[[i]] <- data.frame(num=i, letter=rawToChar(as.raw(64+i))) } print(biglist)
myConcat <- function(u, v) { lenu <- length(u) lenv <- length(v) w = rep(0, lenu + lenv) for(i in 1:lenu) { w[i] = u[i] } for(i in 1:lenv) { w[i + lenu] = v[i] } return(w) } runTimings <- function( ) { df <- data.frame(size=numeric(0), fast=numeric(0), slow=numeric(0)) for(n in seq(50000, 250000, 50000)) { u = runif(n) v = runif(n) times <- system.time(c(u, v)) fast_time = as.vector(times[3]) times <- system.time(myConcat(u, v)) slow_time = as.vector(times[3]) df <- rbind(df, data.frame(size=n, fast=fast_time, slow=slow_time)) } return(df) }
kids <- read.table("c:/datasets/kids1.txt", header=T) kids[order(kids$name), ] kids[order(kids$age), ] kids[order(-kids$age), ] kids[order(kids$gender, -kids$age, kids$name), ]
plot(NULL, NULL, xlim=c(0, 10), ylim=c(0, 10), xlab="x-axis", ylab="y-axis") points(c(2, 5, 8), c(3, 1, 8), pch=16, col="red") lines(c(1, 1, 8), c(9, 1, 8)) text(2, 1, "Start Point")
> str(kids) 'data.frame': 2 obs. of 3 variables: $ name : Factor w/ 2 levels "Alice","Bob": 1 2 $ gender: Factor w/ 2 levels "F","M": 1 2 $ age : int 11 12The summary function is similar. It summarizes the data in an object:
> summary(kids) name gender age Alice:1 F:1 Min. :11.00 Bob :1 M:1 1st Qu.:11.25 Median :11.50 Mean :11.50 3rd Qu.:11.75 Max. :12.00
(function(x)sum(x^2))(1:10)(The normal way to use such a function is like this:
sumOfSquares <- function(x) { sum(x^2) } print(sumOfSquares(1:10))Give an example of where anonymous functions are useful in R.
v <- c(45, 31, 19, 76, 111)using methods 1 and 4. Verify your results by hand.
19 21 45 76 111Compute the 0.67 quartile = Q(0.67) using Method 1:
Index: | 1 | 2 | 3 | 4 | 5 |
Quantile: | 0.2 | 0.4 | 0.6 | 0.8 | 1.0 |
Value: | x1=19 | x2=21 | x3=45 | x4=76 | x5=111 |
> quantile(v, 0.67, type=1) 67% 76 > quantile(v, 0.67, type=4) 67% 55.85 >Here is the R script that draws the interpolation diagram.
cex cex.axis cex.lab cex.main cex.sub col lty main pch sub type xlim xlog ylim ylog
data.frame Date ts
Density | Prob(X ≤ s) | Height of Density | Quantile: q such that Prob(X ≤ q) = p | Outcome of Random Variable |
---|---|---|---|---|
Normal | pnorm(s, m, sd) | dnorm(x, m, sd) | qnorm(p, m, sd) | rnorm(n, m, sd) |
Uniform | punif(s, a, b) | dunif(u, a, b) | qunif(p, a, b) | runif(n, a, b) |
Exponential | pexp(s, r) | dexp(t, r) | qexp(p, r) | rexp(n, r) |
> print(pnorm(1:3, mean=0, sd=1) - pnorm(-(1:3), mean=0, sd=1)) [1] 0.6826895 0.9544997 0.9973002
> print(qnorm(0.975, mean=0, sd=1) [1] 1.959964
> u <- runif(100000, 3, 5) > print(sd(u)) [1] 0.5777419If U is a uniform(3, 5) random variable, the theoretical variance is
u = rnorm(40, mean=0, sd=1) v = rnorm(40, mean=0, sd=1)
w = 0.6 * u + sqrt(1 - 0.6^2) * vRecall that
# Create two standard normal # independent vectors. u = rnorm(40, mean=0, sd=1) v = rnorm(40, mean=0, sd=1) # Create a vector w that has a # correlation of 0.6 with u, such # that w is still standard normal. w = 0.6 * u + sqrt(1 - 0.6^2) * v # Set means and standard # deviations to specified values. mux = 2 sigmax = 3 muy = 1 sigmay = 2 # Scale x and y so that they # have the correct means and SDs. x = sigmax * u + mux y = sigmay * w + muy # Plot the resulting points defined # by the vectors x and y. plot(x, y)