1
00:00:00,660 --> 00:00:04,330
My name is Ashish Mahabal and
we'll be looking further at R ding'g.

2
00:00:06,720 --> 00:00:08,550
So let's start with the assignments.

3
00:00:08,550 --> 00:00:13,310
We saw last time that you can use
the less than arrow and dash to do

4
00:00:13,310 --> 00:00:16,500
the assignment that is shown in the second
line here and that is what is preferred.

5
00:00:16,500 --> 00:00:20,790
So here we show that,
you are assigning 3.14 to z.

6
00:00:20,790 --> 00:00:26,560
As, we said before doing z =
3.14 is also possible but,

7
00:00:26,560 --> 00:00:28,730
you should at best avoid that.

8
00:00:28,730 --> 00:00:32,110
And there are a couple of good reasons for
that and one of the reasons that

9
00:00:32,110 --> 00:00:36,730
people feel that using two key
strokes is not a good idea but

10
00:00:36,730 --> 00:00:40,120
most editors that go with r
allow you to use the less

11
00:00:40,120 --> 00:00:44,850
than dash including the spaces on the side
with the single key stroke so you should.

12
00:00:44,850 --> 00:00:48,390
Get into that soon in emags
as well as in our studio.

13
00:00:48,390 --> 00:00:49,780
There are keystrokes available for that.

14
00:00:50,900 --> 00:00:57,100
But the main reason is that,
equal to gets used for keywords.

15
00:00:57,100 --> 00:00:59,130
Look at the, next line.

16
00:00:59,130 --> 00:01:01,630
We saw an example of rnorm before too.

17
00:01:01,630 --> 00:01:05,700
Here we are taking an rnorm,
100 numbers in that vector and

18
00:01:05,700 --> 00:01:08,270
there we say mean equal to five.

19
00:01:08,270 --> 00:01:09,870
So, here the keyword is mean and

20
00:01:09,870 --> 00:01:12,960
we are saying that we want
the mean of five for that vector.

21
00:01:14,210 --> 00:01:18,860
Here you cannot use the less than dash
equal to is reserved specifically for

22
00:01:18,860 --> 00:01:25,060
that and then people tend to get confuse
if you say a equal to rnorm 100 and so on.

23
00:01:25,060 --> 00:01:27,810
Plus, there are two other things.

24
00:01:27,810 --> 00:01:30,701
If you want to use global variables,
and we'll be seeing an example in

25
00:01:30,701 --> 00:01:33,520
the next slide, there you have to
use less than, less than, dash.

26
00:01:33,520 --> 00:01:39,130
So the next line shows how you
are assigning seven to the vector y.

27
00:01:39,130 --> 00:01:43,590
More than global, it's actually
an assignment to, the enclosing scope.

28
00:01:43,590 --> 00:01:46,260
So whatever scope encloses
that particular statement,

29
00:01:46,260 --> 00:01:49,190
that particular variable gets
that in that particular.

30
00:01:49,190 --> 00:01:50,220
And what I meant.

31
00:01:50,220 --> 00:01:53,210
And of course if you are using equal to,

32
00:01:53,210 --> 00:01:57,030
you still can not use equal to
equal to do the global assignment.

33
00:01:57,030 --> 00:02:00,690
Because equal to equal is of course for
checking something.

34
00:02:00,690 --> 00:02:04,760
So z = = 3 is where you
are trying to check for z = 3.

35
00:02:04,760 --> 00:02:07,260
So that can get quite confusing, so

36
00:02:07,260 --> 00:02:10,880
you should always try to use less
than dash, for the assignment.

37
00:02:10,880 --> 00:02:14,820
And then for global of course, you have to
use less than, less than dash and then for

38
00:02:14,820 --> 00:02:19,210
keywords equal to and for checking
you have to use the two equal tos.

39
00:02:19,210 --> 00:02:24,850
So, get that get into that habit as
soon as you can and stay that way.

40
00:02:26,040 --> 00:02:30,250
So this is how, when we use in
closing scope a variable set.

41
00:02:30,250 --> 00:02:33,390
So if at the prompt, you simply say, bar,

42
00:02:33,390 --> 00:02:35,420
where you are trying to
find out the value of bar.

43
00:02:35,420 --> 00:02:39,070
And if bar has not been set before,
then it's going to return an error,

44
00:02:39,070 --> 00:02:41,500
that, I don't know what bar is.

45
00:02:41,500 --> 00:02:44,820
But then, what you can do,
is that you can set bar in different ways.

46
00:02:46,020 --> 00:02:49,150
Here we see also how to
define a simple function.

47
00:02:49,150 --> 00:02:53,010
And, the function is defined
with the key word function, and

48
00:02:53,010 --> 00:02:54,910
then it is followed by parentheses.

49
00:02:54,910 --> 00:02:58,490
And, within the parenthesis you can give
it the arguments that you wanted to give.

50
00:02:59,750 --> 00:03:03,520
And, the return value is the object foo,
in this case.

51
00:03:03,520 --> 00:03:06,740
So, that is the name of
the function that we'll be using.

52
00:03:06,740 --> 00:03:09,910
And all that we are doing within
the function in this particular case is

53
00:03:09,910 --> 00:03:13,880
setting bar to 1, but
what we are saying is that

54
00:03:13,880 --> 00:03:19,400
set the bar equal to 1 in the enclosing
variable, in the enclosing scope.

55
00:03:19,400 --> 00:03:24,150
So if you now come out of the function
definition and if you run foo.

56
00:03:24,150 --> 00:03:26,740
Then it's going to do that setting for
you.

57
00:03:26,740 --> 00:03:29,240
And then if you ask what varies,

58
00:03:29,240 --> 00:03:31,580
the variable's not defined
outside the function.

59
00:03:31,580 --> 00:03:34,080
You'll get the appropriate value for
bar equal to 1.

60
00:03:34,080 --> 00:03:38,620
So again less than less
than dash will set the,

61
00:03:38,620 --> 00:03:42,150
variable in a higher in closing scope.

62
00:03:42,150 --> 00:03:45,150
And that is what you should do
when you need to do that well.

63
00:03:45,150 --> 00:03:46,920
Avoiding something like
that is always good,

64
00:03:46,920 --> 00:03:51,020
but that is the facility
that is available.

65
00:03:51,020 --> 00:03:54,570
Then again, we can assign entire
vectors using the combine, so

66
00:03:54,570 --> 00:03:56,130
we saw this example before,

67
00:03:56,130 --> 00:04:00,730
how you can assign several different
values using combine to variable x.

68
00:04:00,730 --> 00:04:06,000
So what is actually happening here,
is a specific function called assign is

69
00:04:06,000 --> 00:04:09,380
being called and
the function assign text to arguments.

70
00:04:09,380 --> 00:04:12,810
The first argument is the variable name
that you are giving, x in this case, and

71
00:04:12,810 --> 00:04:18,190
the second argument is a combined set
based on whatever inputs you have given,

72
00:04:18,190 --> 00:04:20,530
so that is what is into a living core.

73
00:04:20,530 --> 00:04:23,840
There's another quirk with
assignment that you can do in R and

74
00:04:23,840 --> 00:04:26,800
that is another thing that you
should avoid as best as possible.

75
00:04:26,800 --> 00:04:29,160
You can do an assignment on
the right hand side also.

76
00:04:29,160 --> 00:04:33,150
So you can say combine these five
values for me and use a dash and

77
00:04:33,150 --> 00:04:35,560
greater than and then a variable in there.

78
00:04:35,560 --> 00:04:38,730
That'll work equally well as the first,
argument, but

79
00:04:38,730 --> 00:04:42,800
again, you shouldn't use that unless
you have to for some specific reason.

80
00:04:44,730 --> 00:04:50,710
And then, you can easily work on entire
vectors as if they were variable,

81
00:04:50,710 --> 00:04:55,310
so if you have assigned five values
that combine to x, you can do 1 by x and

82
00:04:55,310 --> 00:04:58,600
get reciprocals of all those numbers.

83
00:04:58,600 --> 00:05:01,500
Similarly, you can combine
vectors with each other.

84
00:05:01,500 --> 00:05:05,560
Here in the next line we see that
we took the vector x, a zero, and

85
00:05:05,560 --> 00:05:09,430
the vector x again, combined them so
we had five values in x and

86
00:05:09,430 --> 00:05:11,810
this combination is
going to have 11 values.

87
00:05:11,810 --> 00:05:13,750
So those 11 values can be assigned to y.

88
00:05:15,020 --> 00:05:18,610
Similarly, you can use the function,
repeat.

89
00:05:18,610 --> 00:05:22,920
And now I'm trying to assign to v,
a combination of things here.

90
00:05:22,920 --> 00:05:27,660
I'm saying that repeat x and
add to that y and add to that one.

91
00:05:27,660 --> 00:05:30,640
So one of course is an atom and

92
00:05:30,640 --> 00:05:34,290
y is something that has 11 values,
whereas x has five values.

93
00:05:34,290 --> 00:05:35,370
So what it's going to do,

94
00:05:35,370 --> 00:05:40,060
is, it's going to try to repeat
x as many times as necessary.

95
00:05:40,060 --> 00:05:42,930
So that its length equals the longest
of the three arguments on

96
00:05:42,930 --> 00:05:44,170
the right hand side.

97
00:05:44,170 --> 00:05:46,090
In this case, y is 11.

98
00:05:46,090 --> 00:05:49,080
So it'll repeat x2 point 2 times.

99
00:05:49,080 --> 00:05:51,240
And then it'll repeat one 11 times.

100
00:05:51,240 --> 00:05:54,220
And so
you know have three sets of 11 values.

101
00:05:54,220 --> 00:05:59,050
And those would be added as the first
one and the first one and so on, and

102
00:05:59,050 --> 00:06:02,625
you'll get a vector v, which will,
again, have 11 values.

103
00:06:02,625 --> 00:06:06,860
[COUGH] And of course you can, combine
many of these things very easily so

104
00:06:06,860 --> 00:06:10,660
in the next one, we are finding
out variance is, we say that,

105
00:06:10,660 --> 00:06:14,220
that the mean of x, over five values.

106
00:06:14,220 --> 00:06:17,570
Then you subtract the mean
from each of the values of

107
00:06:17,570 --> 00:06:21,270
x squared divided by
the link of x minus 1.

108
00:06:21,270 --> 00:06:24,750
So, you can combine those easily,
write those as functions but

109
00:06:24,750 --> 00:06:27,510
all those things are available
as various functions or so.

110
00:06:27,510 --> 00:06:29,720
And remember help is
available whenever you want.

111
00:06:29,720 --> 00:06:32,140
That's simply saying help
sum is going to tell you,

112
00:06:32,140 --> 00:06:33,710
what it is that sum is actually doing.

113
00:06:36,000 --> 00:06:40,350
Another important construct is an R frame.

114
00:06:40,350 --> 00:06:44,170
And an R frame, is easy to read into, so

115
00:06:44,170 --> 00:06:47,218
you can have a simple
space separated file.

116
00:06:47,218 --> 00:06:52,220
So here I am talking about a file
that contains simply three lines.

117
00:06:52,220 --> 00:06:56,820
The first line has num space name,
one space, value two.

118
00:06:56,820 --> 00:06:59,250
And the next two actually
have some values.

119
00:06:59,250 --> 00:07:03,950
One space one point one space three and
two space four point four and space four.

120
00:07:03,950 --> 00:07:04,850
Right?
What you see in

121
00:07:04,850 --> 00:07:06,900
the table on the right hand bottom.

122
00:07:06,900 --> 00:07:09,530
So if you have an ASCII
file of that nature,

123
00:07:09,530 --> 00:07:14,610
then you can read it [INAUDIBLE] into R,
you can simply say read.table.

124
00:07:14,610 --> 00:07:17,270
And the filename in this case
that I'm using is called foo.

125
00:07:17,270 --> 00:07:19,100
So I read in the file, foo.

126
00:07:19,100 --> 00:07:22,350
And I set header equal to true
meaning that the first line there,

127
00:07:22,350 --> 00:07:25,560
is going to define the names
of the columns for me.

128
00:07:25,560 --> 00:07:29,660
And once that is done,
x now contains the entire file and

129
00:07:29,660 --> 00:07:31,440
you can do various things with that.

130
00:07:32,610 --> 00:07:35,960
Objects is a way to look
at the objects that your R

131
00:07:37,490 --> 00:07:40,010
current in a location of R knows about and

132
00:07:40,010 --> 00:07:44,960
if you give it without any arguments just
the empty parentheses then it'll tell you

133
00:07:44,960 --> 00:07:49,990
about all objects that R knows about
in your current invocation of R.

134
00:07:49,990 --> 00:07:53,080
But X is where we have
rate our table into, so

135
00:07:53,080 --> 00:07:55,990
you can also say that tell me what
are the objects available in X.

136
00:07:57,160 --> 00:08:02,250
And then, what it'll show you are the
names of the columns that you have and

137
00:08:02,250 --> 00:08:06,760
if you type simply X then the entire
frame will be printed out for you.

138
00:08:07,870 --> 00:08:10,550
Name1, is one of the variables there.

139
00:08:10,550 --> 00:08:15,070
If I simply say Name1 at the R prompt and
each of the commands here that

140
00:08:15,070 --> 00:08:18,710
you see are something that
you can give on the R prompt.

141
00:08:18,710 --> 00:08:20,760
So if you say simply Name1,

142
00:08:20,760 --> 00:08:25,150
it's not going to give you anything back
because it doesn't know about Name1.

143
00:08:25,150 --> 00:08:28,440
The Name1 happens to be
inside the object X.

144
00:08:28,440 --> 00:08:33,400
And to access objects within another
object, you have to use the dollar symbol.

145
00:08:33,400 --> 00:08:37,480
So X dollar Name1, is what will
give you the Name1 column there.

146
00:08:38,670 --> 00:08:40,330
That is where namespaces come in.

147
00:08:40,330 --> 00:08:43,970
That's another thing that we had, talked
about in the best programming practices so

148
00:08:43,970 --> 00:08:47,830
you should try not to modify
things by combining namespaces.

149
00:08:47,830 --> 00:08:49,410
But, if you have to do that or

150
00:08:49,410 --> 00:08:52,890
if you're going to use only one name
space, you can do that by saying attach X.

151
00:08:53,970 --> 00:08:57,935
When you do attach X, then the objects
within X become available to you on

152
00:08:57,935 --> 00:09:02,440
the command line or in the enclosing
environment that you're using.

153
00:09:02,440 --> 00:09:06,115
And after that, if you say Name1,
then you'll see the column called Name1.

154
00:09:08,180 --> 00:09:11,140
Redirection, is easy to use in R.

155
00:09:11,140 --> 00:09:12,370
Just like in Unix,

156
00:09:12,370 --> 00:09:17,510
you use the less than to redirect from a
file, or greater than to direct to a file.

157
00:09:17,510 --> 00:09:19,848
Similarly, in R you can use source and
sink.

158
00:09:19,848 --> 00:09:23,600
So source myfile.R will
read from that file

159
00:09:23,600 --> 00:09:26,380
already with the commands
that you have given the, and

160
00:09:26,380 --> 00:09:30,400
sink outfile will write to file
whatever output that you are creating.

161
00:09:31,420 --> 00:09:32,950
Just like the unix command T,

162
00:09:32,950 --> 00:09:37,270
you can also sink,
use sink with a keyword called split=TRUE.

163
00:09:37,270 --> 00:09:38,180
So when you do that,

164
00:09:38,180 --> 00:09:42,080
you'll see the output on the command
line as well as save it to a file.

165
00:09:43,310 --> 00:09:46,830
And you can also capture the output,
into a variable,

166
00:09:46,830 --> 00:09:49,310
instead of a file by using capture.output.

167
00:09:49,310 --> 00:09:53,730
So here we are saying that if we want to
capture the output of one example a file

168
00:09:53,730 --> 00:09:57,900
is going to give you, that will be
stored in that particular variable.

169
00:09:57,900 --> 00:10:01,050
We talked a little bit of,
about Rdata and Rhistory.

170
00:10:01,050 --> 00:10:05,160
Rhistory saves the commands
that you have been given so

171
00:10:05,160 --> 00:10:07,490
that you can easily go back to that.

172
00:10:07,490 --> 00:10:11,460
Unless, list for you radius objects.

173
00:10:11,460 --> 00:10:16,320
And you can, use this recursively or
in tandem with other objects.

174
00:10:16,320 --> 00:10:19,850
So the last line here shows
how you can say that,

175
00:10:19,850 --> 00:10:23,363
assign the output of LS,
the objects that you had.

176
00:10:23,363 --> 00:10:27,990
To list, then you can remove
everything that is in list.

177
00:10:27,990 --> 00:10:31,400
Of course, you shouldn't try to do that in
a script, because then whatever you have

178
00:10:31,400 --> 00:10:34,520
done up to that point in the script,
is going to get erased.

179
00:10:36,130 --> 00:10:38,690
So in R, everything returns something.

180
00:10:38,690 --> 00:10:41,020
In that sense, everything is a function.

181
00:10:41,020 --> 00:10:44,700
Then other things to remember is that
spaces do not matter, but capitalization

182
00:10:44,700 --> 00:10:50,270
does, so if you are using capitals in some
cases you have to continue using those.

183
00:10:50,270 --> 00:10:52,590
Parameters often have odd names.

184
00:10:52,590 --> 00:10:54,140
They can be named,
that's a very good thing,

185
00:10:54,140 --> 00:10:58,670
but because of the organic nature
of how different packages are,

186
00:10:58,670 --> 00:11:02,200
some of them can have fairly odd names,
so you should get used to those.

187
00:11:02,200 --> 00:11:03,540
You can use NA for

188
00:11:03,540 --> 00:11:09,130
missing data and is.na with empty
parenthesis is a very useful construct.

189
00:11:09,130 --> 00:11:11,870
So you should,
try to calculate that early on.

190
00:11:11,870 --> 00:11:16,660
So it does the corresponding test to
check whether a particular point is

191
00:11:16,660 --> 00:11:18,200
available or not.

192
00:11:18,200 --> 00:11:21,460
We saw X reading the table.

193
00:11:21,460 --> 00:11:24,940
There we saw how you can read
it with the header information.

194
00:11:24,940 --> 00:11:27,170
But you can also specify the column names.

195
00:11:27,170 --> 00:11:28,790
In the command line itself.

196
00:11:28,790 --> 00:11:30,800
So all these functions you
should look up the help.

197
00:11:30,800 --> 00:11:33,840
So help read.table will
give the details of

198
00:11:33,840 --> 00:11:36,350
a large number of key words
that are available with it.

199
00:11:37,470 --> 00:11:41,550
If you don't give the names through the
header R through the command line called

200
00:11:41,550 --> 00:11:45,890
read dot table, you can always give names
later on by simply by saying names of

201
00:11:45,890 --> 00:11:49,970
X equal to and give whatever names you
want by combining the, combining function.

202
00:11:51,400 --> 00:11:54,420
Then the large number of standard
functions are available, the plus,

203
00:11:54,420 --> 00:11:59,840
minus power etcetera are their various
trigonometric once are available and

204
00:11:59,840 --> 00:12:02,580
then there are the function
like sort.list and order or so.

205
00:12:03,770 --> 00:12:07,540
The pmax and pmin are interesting
in that they return vectors.

206
00:12:07,540 --> 00:12:12,400
So, if you had a data frame which
had numbers and you say the max of

207
00:12:12,400 --> 00:12:15,760
that data frame, it won't worry
about the different columns and

208
00:12:15,760 --> 00:12:19,780
rows, it'll combine all of them as if it
was a vector and give you a single number.

209
00:12:19,780 --> 00:12:20,310
On the other hand,

210
00:12:20,310 --> 00:12:24,250
if you use pmax, then its going to
give you a column, y is maximum.

211
00:12:24,250 --> 00:12:26,900
And there are various other
such functions available also.

212
00:12:28,270 --> 00:12:31,970
Square root as you can see here we are
getting a square root of a complex number.

213
00:12:31,970 --> 00:12:33,160
So it's clearly overloaded.

214
00:12:33,160 --> 00:12:36,590
Because you can use the same square
root for just ordinary numbers and,

215
00:12:36,590 --> 00:12:37,640
get it equally well.

216
00:12:38,670 --> 00:12:41,160
Sequences are another important construct.

217
00:12:41,160 --> 00:12:45,330
Simply doing 1:30,
gives you the numbers one through 30.

218
00:12:45,330 --> 00:12:49,640
Again, R starts with one, not with zero so
you should remember that.

219
00:12:49,640 --> 00:12:53,360
In this case, colon is also a function.

220
00:12:53,360 --> 00:12:56,420
So, when you say 1:30,
that function is being invoked.

221
00:12:56,420 --> 00:13:02,520
And, colon binds in a strong way,
so if you do something like assign

222
00:13:02,520 --> 00:13:07,710
ten to n and then say that I want segments
one going from one, two and minus one,

223
00:13:07,710 --> 00:13:11,860
using the colon function, then the colon
is going to bind, so you're going to

224
00:13:11,860 --> 00:13:16,360
get one colon and first and then one bind
will be subtracted from each of them.

225
00:13:16,360 --> 00:13:20,010
So, if you want to do something slightly
different you may want to enclose n minus

226
00:13:20,010 --> 00:13:25,490
in two bracket, similarly 2*1:15
is going to give you 2,4 and

227
00:13:25,490 --> 00:13:27,292
all multiples of two, up to 30.

228
00:13:29,190 --> 00:13:34,460
So, sequences can also of course take name
parameters you can use from and to and

229
00:13:34,460 --> 00:13:39,650
by and length, so here you can have
sequence of length 50 when starting from

230
00:13:39,650 --> 00:13:44,780
minus five with jumps of point two,
that's a trivial thing to do.

231
00:13:44,780 --> 00:13:49,380
You can also use a repeat with that so
sequence of five we are assigning.

232
00:13:49,380 --> 00:13:54,620
Add to that a repetition of x times
sequence of five so it's going to take,

233
00:13:54,620 --> 00:13:58,380
it's going to give you x, and then another
copy of x, then another copy of x.

234
00:13:58,380 --> 00:14:02,360
Instead of that, if you wanted
repeats of individual elements first,

235
00:14:02,360 --> 00:14:05,540
then you can do each equal to five
rather than times equal to five.

236
00:14:05,540 --> 00:14:09,290
So again,
doing just help repeat is going to

237
00:14:09,290 --> 00:14:11,690
give you all the good
details of what is available.

238
00:14:12,730 --> 00:14:15,640
Another important construct
is the logical vectors.

239
00:14:15,640 --> 00:14:20,370
So you can assign to n x, but
only where x is greater than 13.

240
00:14:20,370 --> 00:14:22,160
So, what this is going to do,

241
00:14:22,160 --> 00:14:27,400
is it's going to find out which all values
are greater than 13, assign them to TRUE,

242
00:14:27,400 --> 00:14:32,400
assign TRUE to them, assign FALSE to
the others, and then write the vector n.

243
00:14:32,400 --> 00:14:35,770
So the vector n is going to have
the same length as the vector x.

244
00:14:36,930 --> 00:14:41,400
You can similarly do intersection
by using and, union using R, and

245
00:14:41,400 --> 00:14:42,780
negation using bank.

246
00:14:43,980 --> 00:14:48,600
Remember also that FALSE becomes zero and
TRUE becomes one when you coerce them and

247
00:14:48,600 --> 00:14:53,170
you say that, okay, you want numbers
rather than the logical values.

248
00:14:53,170 --> 00:14:57,660
And then similarly, missing values,
you can use them wherever you want to.

249
00:14:57,660 --> 00:14:59,670
And that's another very
important aspect of R.

250
00:15:02,280 --> 00:15:05,450
Indexing is also a very
trivial thing to use.

251
00:15:06,480 --> 00:15:10,560
Just square brackets and 1:10 is going
to give you the first ten values of it.

252
00:15:10,560 --> 00:15:13,240
Similarly, if you want to leave
out something, that's easy also.

253
00:15:13,240 --> 00:15:16,460
Just minus and
then in round brackets 1:5 will do that.

254
00:15:17,670 --> 00:15:20,110
Replacing missing values.

255
00:15:20,110 --> 00:15:24,390
That's something that Is needed very
frequently when you deal with data sets.

256
00:15:24,390 --> 00:15:28,560
So you use the function
is.na(x) that finds out for

257
00:15:28,560 --> 00:15:31,640
you where that particular thing is TRUE.

258
00:15:31,640 --> 00:15:33,660
And then, only for those values.

259
00:15:33,660 --> 00:15:34,550
You assign zero.

260
00:15:34,550 --> 00:15:39,360
So immediately all the, missing values
will be separate, replaced by zero.

261
00:15:39,360 --> 00:15:44,220
And then next line shows how absolute y
can be defined in a different way also.

262
00:15:45,880 --> 00:15:49,800
Now, z assigning to z
something like 6,7,8,

263
00:15:49,800 --> 00:15:54,239
that vector, and then trying to
replace only one value out of that.

264
00:15:55,270 --> 00:16:00,120
What it's going to do, is that it'll
make the 6, 7, 8 into 6, 7, 5.

265
00:16:00,120 --> 00:16:03,570
But when that is done,
a copy of z is first made.

266
00:16:03,570 --> 00:16:06,650
And in the copy,
you replace the third one with 5.

267
00:16:06,650 --> 00:16:08,310
And when this is being done,

268
00:16:08,310 --> 00:16:12,920
actually, the square bracket less than
dash is being used as a function.

269
00:16:12,920 --> 00:16:17,440
And, so again many many functions are used
in very interesting ways internally.

270
00:16:19,130 --> 00:16:21,830
Similarly you can go to arrays and
matrices.

271
00:16:21,830 --> 00:16:27,120
If you simply say c(3,5,100) you are going
to get a vector with those three values.

272
00:16:27,120 --> 00:16:30,952
But instead of that if you assign
that to dim(z) then you are going to

273
00:16:30,952 --> 00:16:32,814
get a 3D array with those sizes.

274
00:16:32,814 --> 00:16:39,307
So you will get an array of size (3,
5, 100) Similarly, you can combine

275
00:16:39,307 --> 00:16:44,899
radius sections all of 3D arrays and
combine them in a preview version.

276
00:16:44,899 --> 00:16:47,570
If you want the entire array,
you can use commas.

277
00:16:47,570 --> 00:16:49,490
So, square brackets and

278
00:16:49,490 --> 00:16:53,350
then separated by empty commas is
going to give the entire array.

279
00:16:53,350 --> 00:16:57,129
Next couple of lines indicate
something quite interesting.

280
00:16:58,370 --> 00:17:03,610
Here, what we are doing is that we take
the numbers one to 20, we make that

281
00:17:03,610 --> 00:17:08,370
into an array, and the key word dim tells
us that it should be a four by five array,

282
00:17:08,370 --> 00:17:10,573
other than, say, a two by ten array.

283
00:17:11,690 --> 00:17:12,810
And we assign that to x.

284
00:17:14,180 --> 00:17:16,930
Then we make another three by two array.

285
00:17:16,930 --> 00:17:21,870
In this case we call it i, and
there we are denoting the values that you

286
00:17:21,870 --> 00:17:26,270
want within the array by 1:3, 3:1 and

287
00:17:26,270 --> 00:17:31,980
those six values now, we say that
arrange them in a three by two damages.

288
00:17:33,940 --> 00:17:39,210
And then if you say something like x of
i and assign zeros to those, then what's

289
00:17:39,210 --> 00:17:45,990
going to happen is that the three by two
array that is in i is like three pairs.

290
00:17:45,990 --> 00:17:49,170
And those three pairs
represent three positions, and

291
00:17:49,170 --> 00:17:52,710
those three positions are looked
up in the array called x and

292
00:17:52,710 --> 00:17:55,280
it is those three positions
that are set to zero.

293
00:17:55,280 --> 00:17:57,912
So in this particular case,
you can work that out as an exercise.

294
00:17:57,912 --> 00:18:03,050
The positions 9, 6, 3 are what
corresponds to what we have set in i,

295
00:18:03,050 --> 00:18:06,231
and those values within
x will get set to 0.

296
00:18:06,231 --> 00:18:08,630
So, you can do fairly complex
things in that version.

297
00:18:08,630 --> 00:18:11,440
And again, for data massaging and

298
00:18:11,440 --> 00:18:14,770
data munching these
functionalities are very useful.

299
00:18:16,530 --> 00:18:19,400
Now, looking that variant,
what you can do is that you

300
00:18:19,400 --> 00:18:23,780
can combine different structures
into a single structure, also.

301
00:18:23,780 --> 00:18:27,540
So, here,
we have something called name, Fred.

302
00:18:27,540 --> 00:18:28,589
Wife, Mary.

303
00:18:28,589 --> 00:18:30,377
Number of children, three.

304
00:18:30,377 --> 00:18:33,290
And child ages, a vector.

305
00:18:33,290 --> 00:18:36,920
So, we have a vector here,
a number here, and some.

306
00:18:36,920 --> 00:18:41,720
Strings, those can all be combined into a
list and assigned to something called Lst.

307
00:18:41,720 --> 00:18:45,230
You should remember that
these are always numbered.

308
00:18:45,230 --> 00:18:51,520
So if you want, the value of, in the
fourth parameter of this particular list,

309
00:18:51,520 --> 00:18:54,950
you can say Lst with two
square brackets of four.

310
00:18:54,950 --> 00:18:57,300
And then it's going to return you.

311
00:18:57,300 --> 00:19:00,790
The vector that is there for child.ages.

312
00:19:00,790 --> 00:19:03,640
And if you want the age
of a particular child,

313
00:19:03,640 --> 00:19:08,840
then you will suffix that list with
two square brackets of four with two.

314
00:19:08,840 --> 00:19:11,410
So that'll give you
the second value of seven.

315
00:19:11,410 --> 00:19:14,200
If you were to leave out that
second pair of square brackets,

316
00:19:14,200 --> 00:19:18,140
that is the enclosing second pair
of square brackets from the four.

317
00:19:18,140 --> 00:19:20,120
It's going to return null to you.

318
00:19:20,120 --> 00:19:22,400
And then, if you were to ask only for

319
00:19:22,400 --> 00:19:24,550
a list of three is going
to give you a number back.

320
00:19:24,550 --> 00:19:28,240
And so,
remember that if you want to get to one of

321
00:19:28,240 --> 00:19:31,310
the specific elements you can use
the square brackets combined with

322
00:19:31,310 --> 00:19:34,880
individual elements within
that particular one.

323
00:19:36,500 --> 00:19:38,300
Then there are matrix operations.

324
00:19:38,300 --> 00:19:41,610
You can simply do A * B,
element by element matrix product or,

325
00:19:41,610 --> 00:19:43,280
you can do matrix multiplication.

326
00:19:43,280 --> 00:19:48,800
Again, look at the slightly quirky
base this is used,% * % for

327
00:19:48,800 --> 00:19:49,870
matrix multiplication.

328
00:19:49,870 --> 00:19:53,950
And there are a large number of
matrix operations that you can do.

329
00:19:53,950 --> 00:19:55,410
I mentioned earlier that re,

330
00:19:55,410 --> 00:20:00,370
reading a file read the table that we did,
you can look at help of that and

331
00:20:00,370 --> 00:20:04,640
you can see lots of of various
keywords that you can use with that.

332
00:20:04,640 --> 00:20:08,900
There are some built in ones to
specifically read.csv, read.csv2 and

333
00:20:08,900 --> 00:20:11,498
so on, so you should also look at that.

334
00:20:11,498 --> 00:20:14,910
Then we'll be, look,
looking at more details about

335
00:20:14,910 --> 00:20:19,440
various other commands and
objects within that.

336
00:20:19,440 --> 00:20:21,190
So, attach, we have already seen here.

337
00:20:21,190 --> 00:20:23,610
I am simply giving
an example of plotting that.

338
00:20:23,610 --> 00:20:25,300
You can read a data set.

339
00:20:25,300 --> 00:20:28,490
And then you can attach those
variables within that and

340
00:20:28,490 --> 00:20:32,390
you can simply plot two
different variables in there.

341
00:20:32,390 --> 00:20:36,650
Pairs is one such example we'll be seeing
more details about plotting later.

342
00:20:36,650 --> 00:20:40,890
So I just wanted to give you a hint of
how to use that within a single command.

343
00:20:40,890 --> 00:20:44,240
So let's not go into
details of that right now.

344
00:20:44,240 --> 00:20:48,190
And, next time we'll be talking
about built in datasets.

345
00:20:48,190 --> 00:20:51,800
I could just be bugging and get to
the basic plotting that I hinted at here.

