> help(hist)A browser window will open with the requested information.
> setwd("c:/csc423/s-files")
> getwd( )
[1] c:/csc423/s-files
> q( )
| Mode | Meaning | Examples |
|---|---|---|
| numeric | Floating Point Numbers | 3.1416, -34.0, 5.97e24 |
| character | Character Strings | "elephant", "abc\n", "345" |
| logical | True or False | TRUE, FALSE, T, F |
| complex | Complex Numbers | 3.43+6.92, 1+0i, 0+1i |
> values = c(33, 5, 429, 37)
> animals = c("dog", "cat", "mouse")
> flags = c(TRUE, FALSE, TRUE, TRUE, FALSE)
> 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
> 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
> 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" "pear"
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.
> 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.
> pdf("mygraph.pdf") # Create PDF file.
> png("mygraph.png") # Create PNG file.
> jpeg("mygraph.jpg") # Create JPEG file.
> bmp("mygraph.bmp") # Create BMP file.
> postscript("mygraph.ps") # Create Postscript file.