author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Tue, 17 Nov 2020 19:49:45 +0200 | |
changeset 2462 | 9b3b29e0b467 |
parent 1943 | 3206ad475871 |
permissions | -rw-r--r-- |
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
1 |
.. -*- coding: utf-8 -*- |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
2 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
3 |
=== |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
4 |
R |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
5 |
=== |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
6 |
.. contents:: |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
7 |
:local: |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
8 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
9 |
Inspecting objects |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
10 |
================== |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
11 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
12 |
Info about object dimensions:: |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
13 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
14 |
length(c(1,2,3)) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
15 |
dim(matrix(1:6, 2, 3)) |
1927 | 16 |
ncol(matrix(1:6, 2, 3)) |
17 |
nrow(matrix(1:6, 2, 3)) |
|
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
18 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
19 |
Brief info about any object:: |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
20 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
21 |
typeof(str) |
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
22 |
class(str) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
23 |
unclass(str) |
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
24 |
str(c(1, 2)) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
25 |
str(summary) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
26 |
|
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
27 |
Column names of datasets:: |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
28 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
29 |
names(...) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
30 |
names(list(colA=1, colB=2)) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
31 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
32 |
Column/row names of matrixes:: |
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
33 |
|
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
34 |
colnames(matrix(...)) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
35 |
rownames(matrix(...)) |
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
36 |
|
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
37 |
List objects in global context: ``ls()``. |
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
38 |
|
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
39 |
Objext size in memory: ``object.site(1:2)`` |
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
40 |
|
1936 | 41 |
Interactive session |
42 |
=================== |
|
43 |
||
44 |
Controlling output precision:: |
|
45 |
||
46 |
options(digits=3) |
|
47 |
||
1939
d56a295b48da
List of all options
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1938
diff
changeset
|
48 |
List of all options:: |
d56a295b48da
List of all options
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1938
diff
changeset
|
49 |
|
d56a295b48da
List of all options
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1938
diff
changeset
|
50 |
str(options()) |
d56a295b48da
List of all options
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1938
diff
changeset
|
51 |
|
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
52 |
Debugging |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
53 |
========= |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
54 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
55 |
To mark function for debugging call:: |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
56 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
57 |
debug(fun, text = "", condition = NULL) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
58 |
debugonce(fun, text = "", condition = NULL) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
59 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
60 |
To return function to normal execution:: |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
61 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
62 |
undebug(fun) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
63 |
isdebugged(fun) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
64 |
|
1927 | 65 |
You can under to debug mode in any piece of code by calling ``browser``. |
66 |
||
67 |
``traceback`` prints out the function call stack after an error occurs; does |
|
68 |
nothing if there's no error. |
|
69 |
||
70 |
``trace`` allows you to insert debugging code into a function a specific places. |
|
71 |
||
72 |
``recover`` allows you to modify the error behavior so that you can browse the |
|
73 |
function call stack. |
|
74 |
||
75 |
Profiling |
|
76 |
========= |
|
77 |
||
78 |
How long execution of expression takes (in low sec/milisec resolution):: |
|
79 |
||
80 |
system.time(expr, gcFirst = TRUE) |
|
81 |
unix.time(expr, gcFirst = TRUE) |
|
82 |
||
83 |
``Rprof`` function enable global profiling. ``summaryRprof`` function decrypt |
|
84 |
profiling data:: |
|
85 |
||
86 |
Rprof() ## start profiling |
|
87 |
Rprof(NULL) ## suspend profiling |
|
88 |
Rprof(append = TRUE) ## resume profiling |
|
89 |
Rprof(NULL) ## end profiling |
|
90 |
summaryRprof() ## investigate profiling report |
|
91 |
||
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
92 |
Generating random numbers |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
93 |
========================= |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
94 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
95 |
For each distribution there are exists corresponding generation function, named |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
96 |
with prefix ``r``:: |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
97 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
98 |
rnorm(n, mean = 0, sd = 1) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
99 |
rt(n, df, ncp) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
100 |
rbinom(n, size, prob) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
101 |
rpois(n, lambda) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
102 |
runif(n, min = 0, max = 1) |
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
103 |
rexp |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
104 |
rchisq |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
105 |
rgamma |
1926
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
106 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
107 |
In order to generate predictable sequences use:: |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
108 |
|
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
109 |
set.seed(seed, kind = NULL, normal.kind = NULL) |
71949d52fd3a
Inspecting objects, Debugging, Generating random numbers
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
110 |
|
1927 | 111 |
Sampling from array:: |
112 |
||
113 |
sample(x, size, replace = FALSE, prob = NULL) |
|
114 |
sample.int(n, size = n, replace = FALSE, prob = NULL) |
|
115 |
||
116 |
sample(1:10, 10) ## permutation!! |
|
117 |
sample(1:10, 100, replace=TRUE) |
|
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
118 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
119 |
Looping over data |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
120 |
================= |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
121 |
|
1936 | 122 |
``lapply`` iterate over data and return list with result of function |
123 |
application:: |
|
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
124 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
125 |
lapply(1:5, function(x) x^2) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
126 |
lapply(matrix(rnorm(20*10),20,10), mean) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
127 |
|
1936 | 128 |
Usually you don't need a list but a vector. ``sapply`` works like ``lapply`` but |
129 |
also try to convert result to matrix or vector is dimantions and elvement types |
|
130 |
permit this:: |
|
131 |
||
132 |
lapply(list(1:5), mean) |
|
133 |
[[1]] |
|
134 |
[1] 3 |
|
135 |
sapply(list(1:5), mean) |
|
136 |
[1] 3 |
|
137 |
||
138 |
``apply`` works on specific dimension of data so useful to work with matrixes |
|
139 |
and data frames:: |
|
140 |
||
141 |
apply(matrix(1:6, 2, 3), 1, min) |
|
142 |
[1] 1 2 |
|
143 |
||
144 |
apply(matrix(1:6, 2, 3), 2, max) |
|
145 |
[1] 2 4 6 |
|
146 |
||
147 |
apply(array(rnorm(2*2*10), c(2, 2, 10)), c(1, 2), mean) |
|
148 |
[,1] [,2] |
|
149 |
[1,] -0.2733804 0.3154234 |
|
150 |
[2,] 0.1830982 -0.5889010 |
|
151 |
||
152 |
``colSums``, ``rowSums``, ``colMeans``, ``rowMeans`` is defined as optimized |
|
153 |
equivalent for:: |
|
154 |
||
155 |
rowSums = apply(x, 1, sum) |
|
156 |
colSums = apply(x, 2, sum) |
|
157 |
rowMeans = apply(x, 1, mean) |
|
158 |
colMeans = apply(x, 2, mean) |
|
159 |
||
160 |
``split`` partitioning data on factor (analog of SQL ``group by``):: |
|
161 |
||
162 |
data<-data.frame(rnorm(10),rbinom(10,1,prob=.7)) |
|
163 |
sdata<-split(data[,1],data[,2]) |
|
164 |
lapply(sdata,mean) |
|
165 |
||
1928
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
166 |
Exploring data |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
167 |
============== |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
168 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
169 |
Check `Inspecting objects`_ section. |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
170 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
171 |
Investigating unique values:: |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
172 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
173 |
sapply(data, unique) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
174 |
sapply(data$col, unique) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
175 |
sapply(data[,c("col1","col2")], unique) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
176 |
sapply(data[,5:10], unique) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
177 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
178 |
table(data$col) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
179 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
180 |
tapply(data$what, data$by, unique) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
181 |
tapply(data$what, data$by, summary) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
182 |
tapply(data$what, data$by, range) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
183 |
tapply(data$what, data$by, mean) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
184 |
tapply(data$what, data$by, sd) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
185 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
186 |
Brief info about vectors and matrixes:: |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
187 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
188 |
summary(1:8) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
189 |
summary(matrix(1:20, 4, 5)) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
190 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
191 |
Simple plots:: |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
192 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
193 |
i<-1:100 |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
194 |
x<-i/10 |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
195 |
y<-x^2 |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
196 |
plot(x,y) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
197 |
|
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
198 |
hist(rpois(100,10)) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
199 |
hist(rpois(100,10),breaks=20) |
eb1e2557ebf7
Looping over data, Exploring data
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1927
diff
changeset
|
200 |
|
1936 | 201 |
Renaming columns |
202 |
================ |
|
203 |
:: |
|
204 |
||
205 |
names(d)[names(d)=="beta"] <- "two" |
|
206 |
names(d)[2] <- "two" |
|
207 |
||
1937 | 208 |
library(plyr) |
1938 | 209 |
newd <- rename(d, c("beta"="two", "gamma"="three")) |
210 |
||
1943
3206ad475871
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1942
diff
changeset
|
211 |
Removing names for raws and columns |
3206ad475871
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1942
diff
changeset
|
212 |
=================================== |
3206ad475871
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1942
diff
changeset
|
213 |
:: |
3206ad475871
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1942
diff
changeset
|
214 |
|
3206ad475871
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1942
diff
changeset
|
215 |
rownames(dt) <- NULL |
3206ad475871
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1942
diff
changeset
|
216 |
colnames(dt) <- NULL |
3206ad475871
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1942
diff
changeset
|
217 |
|
1940
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
218 |
Filtering raws and columns |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
219 |
========================== |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
220 |
:: |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
221 |
|
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
222 |
TODO |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
223 |
|
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
224 |
Droping raws and columns |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
225 |
======================== |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
226 |
|
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
227 |
Drop column from data frame by number:: |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
228 |
|
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
229 |
dfnew <- df[-1] # first |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
230 |
dfnew <- df[-ncol(df)] # last |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
231 |
dfnew <- df[-c(1, 3:4, 7)] # range |
7b82258d60a7
Drop column from data frame by number
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1939
diff
changeset
|
232 |
|
1941
486fcedecf72
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1940
diff
changeset
|
233 |
Drop column from data frame by name:: |
486fcedecf72
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1940
diff
changeset
|
234 |
|
486fcedecf72
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1940
diff
changeset
|
235 |
newdf <- df[ , !(names(df) %in% c("lat", "long"))] |
486fcedecf72
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1940
diff
changeset
|
236 |
|
1942
1435b942faab
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1941
diff
changeset
|
237 |
df <- data.frame( a = 1:10, b = 2:11, c = 3:12 ) |
1435b942faab
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1941
diff
changeset
|
238 |
df <- subset(df, select = c(a,c)) |
1435b942faab
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1941
diff
changeset
|
239 |
df <- subset(df, select = -c(a,c)) |
1435b942faab
Drop column from data frame by name
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1941
diff
changeset
|
240 |