-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSec-Useful.Rnw
146 lines (128 loc) · 2.62 KB
/
Sec-Useful.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
140
141
142
143
144
145
146
\section{Useful functions}
\begin{frame}
\begin{block}{Data IO}
\begin{description}
\item[read.table] creates a \Robject{data.frame} from a spreadsheet file.
\item[write.table] writes a \Robject{data.frame}/\Robject{matrix} to a spreadsheet (tsv, csv).
\item[save] writes an binary representation of \R objects to a file (cross-platform).
\item[load] load a binary \R file from disk.
\end{description}
\end{block}
Specialised data formats often have specific i/o functionality (microarray \texttt{CEL} files, \texttt{XML}, \ldots)
\end{frame}
\begin{frame}[fragile]
<<read.csv0, tidy = FALSE>>=
read.table("./Data/data.csv", sep = ",",
header = TRUE, row.names = 1)
@
\end{frame}
\begin{frame}[fragile]
<<read.csv1>>=
read.csv("./Data/data.csv", row.names = 1)
@
\end{frame}
\begin{frame}[fragile]
<<read.csv2>>=
x <- read.csv("./Data/data.csv", row.names = 1)
save(x, file = "./Data/data.rda")
rm(x)
load("./Data/data.rda")
x[1:3, ]
@
\end{frame}
\begin{frame}[fragile]
\begin{block}{String manipulation (1)}
<<str1>>=
paste("abc", "def", sep = "-")
paste0("abc", "def")
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{String manipulation (2)}
<<str2>>=
month.name[1:4]
grep("Feb", month.name)
grep("Feb", month.name, value = TRUE)
grepl("Feb", month.name)
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{String manipulation (3)}
<<str3>>=
month.name[1]
length(month.name[1])
nchar(month.name[1])
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{String manipulation (4)}
<<str4>>=
strsplit("abc-def", "-")
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Comparing and matching (1)}
<<comp>>=
set.seed(1)
x <- sample(letters[1:10], 6)
y <- sample(letters[1:10], 6)
x
y
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Comparing and matching (2)}
<<comp2>>=
intersect(x, y)
setdiff(x, y)
union(x, y)
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Comparing and matching (3)}
<<comp3>>=
x %in% y
x == y
match(x, y)
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Generating data (1)}
<<gen>>=
seq(1,7,3)
rep(1:2, 2)
rep(1:2, each = 2)
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Generating data (2)}
<<gen2>>=
runif(5)
rnorm(5)
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{About the data}
<<aboutdata>>=
table(sample(letters, 100, replace = TRUE))
summary(rnorm(100))
head(x)
tail(x)
@
\end{block}
\end{frame}
\begin{frame}[fragile]
<<head>>=
M <- matrix(rnorm(1000), ncol=4)
head(M)
@
\end{frame}