170 likes | 232 Views
S-PLUS Lecture 2. Jaeyong Lee Department of Statistics Pennsylvania State University. Naming Conventions. Names in S-Plus are made up of upper- and lower-case alphabets, 0-9, and period, ‘.’, in any noninitial position. S is case sensitive. The underscore, ‘_’, is not available.
E N D
S-PLUS Lecture 2 Jaeyong Lee Department of Statistics Pennsylvania State University
Naming Conventions • Names in S-Plus are made up of upper- and lower-case alphabets, 0-9, and period, ‘.’, in any noninitial position. • S is case sensitive. • The underscore, ‘_’, is not available.
Language Layout • S-Plus commands are either expressions or assignments. 1 – pi +exp(1.7) a <- 6 • Commands are separated by either ‘;’ or a newline. a <- 7; b <- a*7 • ‘#’ marks the rest of line as a comment. a + b # This is an illustration. • If a command is not complete, S-Plus issues ‘+’ rather than ‘>’. • ‘.Last.value’ stores the most recently evaluated non-assignment expression.
Language Layout-Continued • If a command is not complete, S-Plus issues ‘+’ rather than ‘>’. a * exp(3 + • .Last.value stores the most recently evaluated non-assignment expression. 1 – pi + exp(1.7) .Last.value a <- 6 .Last.value
Vectors • In S-Plus, there are no scalars; vectors of length 1 are used instead. • Vectors are made up of numeric, logical values, or character strings. But you can not mix them. • Character strings can be entered with either double or single quotes, but will always be printed with double quotes.
Session: Vectors x <- c(1.2, 2.4, 2.7, 5.6, 7.2) colours <- c(“red”, “green”, “blue”, “white”) x[3] colours[2] x > 3 names(x) <- c(‘a’,’b’,’c’,’d’,’e’) x x[b] x[“b’] letters x[letters[1:3]] x[-(3:5)]
Vector Arithmetic • Arithmetic operations on vectors are performed element by element. • If two vectors in the same expression have different length, the expression will produce a vector with the same length as the longest in the expression. The shorter vectors are recycled until they match the length of the longest.
Session: Vector Arithmetic x <- 1:20 y <- 1:7 2*x sin(x) x+2y + 1 30:1 seq(from=10, to = 1, by=.5) rep(x, times=3)
Logical Vectors • Possible elements of a logical vector is T (True) and F (False). • Logical operators are >, >=, <, <=, ==, !=. • c1&c2 (intersection), c1|c2 (union), !c1 (negation). • A logical vector can be used in ordinary arithmetic, in which case F and T become 0 and 1, respectively.
Session: Logical Vectors x <- 1:30 big <- x > 20 medium <- (x>=10) & (x <=20) sum(x < 10)
Missing Value Marker, NA • When an element of a vector is “not available” or “missing”, the special value NA can be used to reserve the place. • In general, any operation on an NA becomes an NA.
Session: NA x <- 1:5 is.na(x) x[3] <- NA is.na(x)
Session: Character Vector X <- c(“abc”, “def”, “ghi”) paste(“abc”, “def) paste(“abc”, “def”, sep=“”) paste(c(“X”,”Y”),1:10,sep=“”)
Index Vectors • Subsets of elements of a vector can by selected by appending to a name of the vector an index vector in square brackets. • An index expression can appear on the receiving side of an assignment expression. • There are four kinds of index vectors: a logical vector (in this case, the lengths of the vector and index vector must match.) a positive integral vector a negative integral vector a character string vector.
Intrinsic Attributes: mode and length • Every object in S-Plus has two attributes, mode and length. • The mode of a vector can be numeric, complex, logical, and character. • The function attributes(x) gives all non-intrinsic attributes of x. • There is a special attribute called class which was introduced to enhance object-oriented programming. For example, plot() will react a different class differently.
Session: Intrinsic Attributes x <- 1:10 mode(x) length(x) attributes(x) dim(x) <- c(2,5) attributes(x) dim(x) <- c(10) digits <- as.character(x) y <- as.numeric(digits) E <- numeric() E[4] <- 17
x <- 1:10 mode(x) length(x) attributes(x) dim(x) <- c(2,5) attributes(x) dim(x) <- c(10) digits <- as.character(x) y <- as.numeric(digits) E <- numeric() E[4] <- 17 leukemia mode(leukemia) length(leukemia) attributes(leukemia) unclass(leukemia) Session: Intrinsic Attributes