-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSec-Manip.Rnw
141 lines (120 loc) · 2.51 KB
/
Sec-Manip.Rnw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
\section{Manipulating data}
<<env, echo=FALSE, message = FALSE>>=
library(Biobase)
data(sample.ExpressionSet)
options(width = 50)
@
\subsection{Subsetting}
%% Sub-setting (positive and negative indices, logicals, by name), assignments
\begin{frame}
\begin{itemize}
\item One of the most powerful features of \R is its ability to manipulate subsets of vectors and arrays.
\item As seen, subsetting is done with, \Rfunction{[]}, \Rfunction{[, ]}, \ldots
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting with positive indices}
<<sub1>>=
x <- 1:10
x[3:7]
x[9:11]
x[0:1]
x[c(1, 7, 2, NA)]
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Assignments with positive indices}
<<sub2>>=
x[2] <- 20
x[4:5] <- x[4:5] * 100
## x[1:6] ?
@
\pause
<<sub2res>>=
x[1:6]
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting with negative indices}
<<sub3>>=
x <- 1:10
## x[-c(3:7)] ?
@
\pause
<<sub3res>>=
x[-c(3:7)]
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting with logical predicates}
<<sub4>>=
x[c(TRUE, TRUE, rep(FALSE, 8))]
x > 5
x[x > 5]
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting with logical predicates}
<<sub4b>>=
## x[c(TRUE, FALSE)] ?
@
\pause
<<sub4bres>>=
x[c(TRUE, FALSE)] ## recycled
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting by names}
<<sub5>>=
x <- c(a = 1, b = 2, c = 2)
x[c("a", "c")]
x[c("a", "d")]
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting matrices}
<<submat0>>=
M <- matrix(1:12, 3)
M[1, ] ## row -> vector (or drop = FALSE)
M[, 1] ## column -> vector (or drop = FALSE)
M[2,3] <- 0
M
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting matrices (2)}
<<submat1>>=
M < 9
M[M < 9] <- -1
M
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting lists}
<<sublist>>=
ll <- list(a = 1:3, b = "CSAMA", c = length)
ll[1] ## still a list, but of length 1
ll[[1]] ## first element of the list
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Subsetting \Robject{ExpressionSet} instances}
It is reasonable to expect that subsetting operations work also for higher order objects.
<<subexprs>>=
sample.ExpressionSet[1:10, 1:2]
@
\end{block}
\end{frame}
%% \subsection{Learning about objects}
%% \begin{frame}
%% \Rfunction{class}, \Rfunction{str}, \Rfunction{head}/\Rfunction{tail}.
%% \end{frame}