4. R Basic Object Types
• R basic data structure types
– (row) vector (In R, everything is vector)
– matrix
– list
– data.frame
– factor
– environment
• In R the “base" type is a vector, not a scalar.
8. Python dir(obj)
• attributes(obj)
• str(object)
• ls() (Python> dir() )
• The function attributes(object) returns a list of
all the non-intrinsic attributes currently
defined for that object.
9. R attr(object, name)
• The function attr(object, name) can be used to
select a specific attribute.
• When it is used on the left hand side of an
assignment it can be used either to associate a
new attribute with object or to change an
existing one.
• For example
• > attr(z, "dim") <- c(10,10)
– allows R to treat z as if it were a 10-by-10 matrix.
27. Names on Matrix
• Just as you can name indices in a vector you
can (and should!) name columns and rows in a
matrix with colnames(X) and rownames(X).
• E.g.
– colname(R-matrix) <- c(name_1,name_2,…)
– colname(R-matrix) [i] <- name_i
28. Functions on Matrix
• If X is a matrix apply(X, 1, f) is the result of
applying f to each row of X; apply(X, 2, f) to
the columns.
– Python> map(func,py-List)
29. Add Columns and Rows
• cbind
E.g.
> cbind(c(1,2,3),c(4,5,6))
• rbind
E.g.
> rbind(c(1,2,3),c(4,5,6))
31. Explicitly like a list
• When can a list be made into a data.frame?
– Components must be vectors (numeric, character,
logical) or factors.
– All vectors and factors must have the same lengths.
40. repeat
• repeat statement
• The repeat statement causes repeated evaluation of
the body until a break is specifically requested.
• When using repeat, statement must be a block
statement. You need to both perform some
computation and test whether or not to break from
the loop and usually this requires two statements.
42. Create Function in R
• name <- function(arg_1, arg_2, ...) expression
• E.g.
– ADD <- function(a,b) a+b
– ADD <- function(a,b) {c<-a+b}
– ADD <- function(a,b) {c<-a+b;c}
– ADD <- function(a,b) {c<-a+b; return(c)}
– (All these functions are the same functions)
43. Function Return R-List
• To return more than one item, create a list
using list()
• E.g.
– MyFnTest1 <- function(a,b) {c<-a+b;d<-a-b;
list(r1=c,r2=d)}
– MyFnTest1 <- function(a,b) {c<-a+b;d<-a-b;
return(list(r1=c,r2=d))}
– (These two functions are the same, too)
52. Base Graphics
• Use function such as
– plot
– barplot
– contour
– boxplot
– pie
– pairs
– persp
– image
53. Plot Arguments
• type = ???
• axes = FALSE : suppresses axes
• xlab = “str” : label of x-axis
• ylab = “str” : label of y-axis
• sub = “str” : subtitle appear under the x-axis
• main = “str” : title appear at top of plot
• xlim = c(lo,hi)
• ylim = c(lo,hi)
54. Plot’s type arg
• type =
– “p” : plots points
– “l” : plots a line
– “n” : plots nothing,
just creates the axes for later use
– “b” : plots both lines and points
– “o” : plot overlaid lines and points
– “h” : plots histogram-like vertical lines
– “s” : plots step-like lines
56. pch
• 0:18: S-compatible vector symbols.
• 19:25: further R vector symbols.
• 26:31: unused (and ignored).
• 32:127: ASCII characters.
• 128:255 native characters only in a single-byte
locale and for the symbol font. (128:159 are
only used on Windows.)
• Ref: http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/points.html
http://rgraphics.limnology.wisc.edu/
57. cex
• a numerical vector giving the amount by
which plotting characters and symbols should
be scaled relative to the default. This works as
a multiple of par("cex"). NULL and NA are
equivalent to 1.0. Note that this does not
affect annotation: see below.
• E.g.
– points(c(6,2), c(2,1), pch = 5, cex = 3, col = "red")
– points(c(6,2), c(2,1), pch = 5, cex = 10, col = "red")
87. adaboost.M1
• 此函數的演算法使用 Freund and Schapire‘s
Adaboost.M1 algorithm
• 其中 weak learner 的部分使用 CR Tree 也就
是R中的 rpart package
88. adaboost.M1’s Training Data Form
• Label Column must be a factor object
(in source code)
fit <- rpart(formula = formula, weights =
data$pesos, data = data[, -1], maxdepth =
maxdepth)
flearn <- predict(fit, data = data[, -1], type =
"class")