> help(hist)A browser window will open with the requested information.
> setwd("c:/rdatasets") > getwd( ) [1] c:/rdatasets
> quit( )or
> q( )
Mode | Meaning | Examples | Restrictive |
---|---|---|---|
logical | True or False | TRUE, FALSE, T, F | Most |
numeric | Floating Point Numbers | 3.1416, -34.0, 5.97e24 | 2nd Most |
complex | Complex Numbers | 3.43+6.92i, 1+0i, 0+1i | 2nd Least |
character | Character Strings | "elephant", "abc\n", "345" | Least |
> values = c(33, 5, 429, 37) > animals = c("dog", "cat", "mouse") > flags = c(TRUE, FALSE, TRUE, TRUE, FALSE)
x = 318 x <- 318 318 -> x
> x <- 318
> values [1] 33 5 429 37 > animals [1] "dog" "cat" "mouse" > flags [1] TRUE FALSE TRUE TRUE FALSE
> mode(values) [1] "numeric" > mode(animals) [1] "character" > mode(flags) [1] "logical" > length(values) [1] 4 > length(animals) [1] 3 > length(flags) [1] 5
> is.numeric(345) [1] TRUE > is.character(345) [1] FALSE > is.logical(345) [1] FALSE > is.complex(345) [1] FALSE
> c(T, 34, "dog") [1] "TRUE" "34" "dog"
as.character as.complex as.logical as.numericFor example:
> as.numeric("1198") [1] 1198 > as.complex(43) [1] 43+0i > as.logical("FALSE") [1] FALSE > as.logical(13) [1] TRUE
> r = rep(45, 5) > r [1] 45 45 45 45 45
> s = 1:6 > s [1] 1 2 3 4 5 6 > r = 9:6 [1] 9 8 7 6
> q = seq(from=3, to=5.5, by=0.5) > q [1] 3.0 3.5 4.0 4.5 5.0 5.5 > p = seq(from=900, to=600, by=-100) [1] 900 800 700 600
Operators | Description | Precedence |
---|---|---|
( ) { } | Function call and grouping | 1 (High) |
[ ] [[ ]] | Indexing | 2 |
:: ::: | Variable in namespace | 3 |
$ | Component extraction | 4 |
^ | Exponentiation (right to left) | 5 |
+ - | Unary plus and minus | 6 |
: | Sequence | 7 |
%any% %/% %% | Special operator,
integer division, mod | 8 |
* / | Multiplication, division | 9 |
+ - | Addition and subtraction | 10 |
< > <= >= != == | Comparison | 11 |
! | Logical not | 12 |
& && | Logical and | 13 |
| || | Logical or | 14 |
~ | As in formulas | 15 |
<- | Assignment | 16 |
= | Assignment | 17 |
-> | Assignment | 18 |
? | Help (unary and binary) | 19 |
0 / 0 Inf / Inf Inf - Inf Inf * 0
> v = c(4, 2, 5, 9, 4, 7, -1, 6, 0, 3, 2, 7) > m = matrix(v, 3, 4, byrow=TRUE) > m [,1] [,2] [,3] [,4] [1,] 4 2 5 9 [2,] 4 7 -1 6 [3,] 0 3 2 7
Operation | Operator or Function |
---|---|
Matrix Addition | + |
Scalar Multiplication | * |
Matrix Multiplication | %*% |
Transpose | t |
Identity | diag(nrow=n) |
Inverse | solve |
> a = c("apple", "orange", "pear", "grape") > u = c(1, 4) > a[u] [1] "apple" "grape"The values with indices 1 and 4 are selected from a.
> a = c("apple", "orange", "pear", "grape", "watermelon") > v = c(-2, -4, -5) > a[v] [1] "apple" "grape"The values with indices 2, 4, and 5 are omitted from a.
> a = c("apple", "orange", "pear", "grape", "watermelon") > w = c(F, T, F, T, T) > a[w] [1] "orange" "grape" "watermelon"Only the values corresponding to TRUE, which have indices 2, 4, and 5 are selected.
m[5:10, -4]Rows 5 through 10 are selected and column 4 is omitted from the matrix m.
> M = matrix(1:9, 3, 3, byrow=T) > M [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 > M[,3] [1] 3 6 9
> M[,3, drop=F] [,1] [1,] 3 [2,] 6 [3,] 9
Attribute | Description |
---|---|
class | The class of the object |
comment | A comment on or description of the object |
dim | The dimentions of a matrix or array |
dimnames | Names associated with each dimension of an object |
names | The names associated with an object, often row or list item names |
row.names | The row names of a matrix or data frame |
tsp | Start time for a time series data object |
levels | Levels of a factor |
> scores = list(name=c("Jason", "Ginger", Mary), + midterm=c(89, 93, 90)) > scores $name [1] "Jason" "Ginger" "Mary" $midterm [1] 89 93 90 > scores$name [1] "Jason" "Ginger" "Mary" > scores$midterm [1] 89 93 90
name midterm 1 Jason 89 2 Ginger 93 3 Mary 90
> scores = data.frame(name=c("Jason", Ginger", "Mary"), + midterm=c(89, 93, 90))
> # Enter a blank line to terminate input from keyboard: > x = scan( ) 1: 4 2 5 4 6 6: Read 5 items. > x [1] 4 2 5 4 6 > setwd("c:\r-input") > getwd( ) [1] c:\r-input > # Default mode of input is numeric: > x = scan("input.txt") > x [1] 45 65 45 4 > # Set input mode to character: > y = scan("states.txt", what="character") > y [1] "Illinois" "Indiana" "Iowa" "Nebraska"
name midterm Jason 89 Ginger 93 Mary 90These statements read the table data from a file and load them into a data frame:
> scores = read.table("scores.txt", header=TRUE) > scores name midterm Jason 89 Ginger 93 Mary 90 > scores$name [1] "Jason" "Ginger" "Mary" > scores$midterm [i] 89 93 90
name midterm 1 Jason 89 2 Ginger 93 3 Mary 90Read the file like this:
> scores = read.table("scores.txt")
> source("script.r")Caution: entering an expression in a script file will not automatically display the value of the expression. Use the print function to display the value. For example:
> print(mean(x))
> cat("The mean of the vector x is ", mean(x), ".\n")
The mean of the vector 3.54.
> sink("output.txt")
> sink( )to resume sending output to the screen.
help ?
c length
mean sd var cor median quantileProblem: Design a simulation to illustrate how the central limit theorem works with uniform random numbers. Ans: Recall that the Central Limit Theorem states that even if random samples are drawn from a non-normal distribution, the mean of the random samples will have an approximately normal distribution if the sample sizes are high enough (rule of thumb: n > 30). Here is the script:
m = NULL for(i in 1:100) { x <- runif(50) m <- c(m, mean(x)) } # Normal plot of the means: qqnorm(m) # Normal plot of last random sample: qqnorm(x)
abs ceiling floor length prod round sign sqrt sum
acos asin atan atan2 cos sin tanProblem: Use the sin function to evaluate sin π. Also evaluate
> sin(pi) [1] 1.224606e-16 > sin(10^15 * pi) [1] -0.2362051
exp expm1 log log10 log2 log1p
nchar paste substr toupper tolower chartr strtrim strsplit
+ - %*% t diag solve det
apply lapply sapply
> print(A) [,1] [,2] [1,] 1 1 [2,] 1 1 > print(B) [,1] [,2] [1,] 2 2 [2,] 2 2 > cbind(A, B) [,1] [,2] [,3] [,4] [1,] 1 1 2 2 [2,] 1 1 2 2 > rbind(A, B) [,1] [,2] [1,] 1 1 [2,] 1 1 [3,] 2 2 [4,] 2 2
> system.time(rnorm(1000)) user system elapsed 0.00 0.00 0.01 > system.time(rnorm(100000)) user system elapsed 0.01 0.00 0.02 > system.time(rnorm(10000000)) user system elapsed 1.15 0.04 1.19rnorm(n) generates a vector of n independent standard normal random variables. Standard normal means mean=0 and standard deviation=1.
pnorm punif pbinom ppois pexp dnorm dunif dbinom dpois dexp rnorm runif rbinom rpois rexp
square <- function(x) { y <- x^2 return(y) }
square <- function(x) { y <- x^2 y } # or square <- function(x) { x^2 }
> square(13) [1] 169
> v <- 1:4 [1] 1 2 3 4 > second(v) <- 5 [1] 1 5 3 4
`second<-` = function(v, value) { v[2] = value return(v) }Note that back quotes are needed to define the second<- function.
`%.%` <- function(u, v) { return(sum(u * v)) }
> a <- 1:4 > b <- 1:4 > a %.% b [1] 14
> days <- factor(c('Wed', 'Thu', 'Sat', 'Mon')) > days [1] Wed Thu Sat Mon Levels: Mon Sat Thu Wed
> days.of.week <- c('Sun', 'Mon', 'Tue', 'Wed', + 'Thu', 'Fri', 'Sat') > days <- factor(c('Wed', 'Thu', 'Sat', 'Mon'), + levels=days.of.week) > days [1] Wed Thu Sat Mon Levels: Sun Mon Tue Wed Thu Fri Sat
as.numeric(days) [1] 4 5 7 2
> time <- c(65, 68, 66, 31) > plot(days, time)