My name is Ashish Mahabal, and we'll continue to look at R today. Today we'll be looking at classes in R. And as we know, R has come from S, and the nomenclature that was used in S has continued with R classes. So, S3 and S4 are the two main classes, that are used in R. Google has a style guide on how to use our classes, and they recommend that S3 is much better than S4, though S4 ones are more rigorous, and we'll be seeing more details about that. But one important thing that is said by everybody respected for that they say S3 are better than S4 or S4 are better than S3, is that you should try not to mix the two classes, because S4 are definitely more rigorous and S3 are more burdening, but again if you are not expecting some kind of behavior. That comes about in S4 and S3 can lead you to grieve. So, there are two more blood types of classes, the base class where typically C is used, and referenced classes where in-place modification is possible. We may not be seeing more details of that, but the URL that is given here, will provide you more details if you are interested in something like that. So let's start with S3 classes, they have existed for long time the coalition [INAUDIBLE] came about, essentially what an S3 class has is a list, and some method and inheritance that can be done at that. And then it's a bit more generic and casual very unlike what a class would be in C++. In particular there's no way of checking, so if you have certain variables that exist in your class and you make a typo and during some assignment provide a different variable name. It will happily take that and will not even tell you that, that is not what you had first given, et cetera. So, you had to be sure that, that is what you'll be wanting. Let's try to define an anastry class. Let's say that we have a few galaxies and we want to define the class called Galaxy. So here, you see that we have a variable g and we make it a vector by assigning a list to it. We provide some named variables. Name equal to say NGC 4261, RA such and such Declination such and such. RA and Declination are the two variables that tell you the position of the object. Galaxy in this case on the sky just like latitude and longitude. And then there is a certain magnitude. And then once you have defined this variable called g, then you can say that class of g is Galaxy. And just doing these two things together defines for you that class. Now you can go ahead and check the attributes of the classes. G is an example, you can say attributes of g, and then it'll immediately tell you what are the names of a label with g. And those are in this case, Name, RA, Declination and mag. And then it'll go ahead and tell you that the class is Galaxy, that it belongs to the class Galaxy. And then you can print info about the particular object. So, if you were to simply say, g, which is equivalent to, say print g, then it will tell you that the name that is available is NGC 4261. The RA value is given there, the declination value is given there, and the magnitude is given there. And that the attribute of the class is Galaxy. So, one of the most interesting thing what we alluded to in an earlier talk is how we can change some of the default methods. So, in this case, if we wanted to change the print method, then we can do that fairly easily. Here we saw that if you simply say print GRG, you get the default print values. But that's not what we want to print, say. Suppose we wanted to take a look at where the Galaxy is, try to get a sky map of it. One can easily do that in R, using how the matrix can be overloaded or modified for this specific case. In particular, let's look at one particular, one cla, one method called browseURL, which takes as an argument, URL and simply goes to that particularly URL software, like in other cases, takes many different arguments, but, in this case, we'll just worry about the single argument called the URL. So, let's define a new print class for our Galaxy. To display an image. So, the definition of the function is given on the slide. So, if you say print.galaxy, that is what our method is going to be. Print is the default name, and .galaxy tells us that it is for the class galaxy that we are defining that. And the function takes as an argument just the object for which we are going to be talking about, in this case let's say g. And then we define within the function a base URL, which can be the URL that we want to go to. And the URL is going to depend on two variables in this case, the RA and declaration for the galaxy. So then we got the name of the Galaxy which will get printed on the command, on the output. And then we paste the baseurl, the ra with its value, the dec with its value. And the last command is also the last argument that's also very important separator without any spaces, because the default is a space. So when you paste those together and assign that to g_url, then you can simply call browseURL with that argument g_url. And then your define, your print function has been defined for that class. Now, if you look at meters of this particular class Galaxy, then you'll find that you are told that it does have one method called plain.galaxy. And now if you go ahead and say g, or print of g, you will see that the gives you NGC4261 on the output. But at the same time, it opens a browser. And in the browser, you in fact see the position in sky at that particular that you have said that the object g belongs to. And that is what you see on the right hand side. This is a slow image of NGC 4261. So again, this is an S3 class that we have defined and a specific print method with it. If we had misspelled any of the attribute names, for instance, in declination. If we had spelled it with. The lowercase of our RA, lowercase ra, then the program would have accepted that. Would not have given us any error. But just that there wouldn't have been any useful output. So that is one thing that you are to remember. Now S4 objects, are more rigorous. Here, the way they are defined is with a slightly, writing a slightly different syntax. Use a set class. Here, we are using the class name to be GalaxyS4. And then you have a representation for it. Again, we'll use the same variable names. But here, we tell what types those variables are. Remember, earlier, when we used with S3. When we defined the class, we didn't have to say anything of that kind. We just went ahead and defined the list. Whatever that list contained. But here, we are to say that the name is of type character and the other three variables are of type numeric. And that's again, all for the definition. Then you can say that gS4. And assign to it a new GalaxyS4, and then you give it the data through the names and variables there. So we're going to see 4260, same as before, right? And then that defines for you an S4 type of class. In this case GS4 is an example. So, now if you go in and say gS4, then it'll tell you that it's an object of class GalaxyS4, and now what it uses are what are called slots. So it says that it has four slots. One is the name, and RA, declination and magnitude. And these slots, in S4 are accessible using the at character. Remember in S3, we had the dollar character. So, you could have said g$Name and got back NGC 4261. To do that with S4 classes, what you are to do is gS4@Name. And you'll get the same information. There are these minor differences, but the main difference, of course, is error checking involvement, the rigorousness with which the class itself is defined. So, what is print after all which we have managed to in this fashion. So, it completely depends on the context in which it is being used. So, if you are simply to say print at the R prompt, you'll be told that it is a function which takes x's and argument and dot, dot, dot indicating that there are many more arguments. And all that is happening is the use method print is being called. And so, when you says print galaxy without any arguments despite self, it does so how that particular function is defined. That is another very useful thing in R. If there is a function and if you simply give the function name, it provides for you the party of the function, so you can take a look at any function that R is using at any time, just by typing the name of that function. So here we see the definition R for [COUGH] function print Galaxy. So there are many different prints available and all you are to do is say methods print on the R prompt and you will get to know that there are over 100 or 200 prints that are accessible to you. The ones with stars are typically in other named spaces. So you may have to do something specific to get access to those. But so take a look at the let's see, print.by, which is the 18th one, the last one in this row. Our print.aspell which is thirteenth here. So the aspell comes with an asterisks, which tells you that if you have to simply say print out aspell you won't get imager access to it, but you have to do something different. When it's print.by, it's without an asterisk, so you can simply say printed by, and you'll get access to that. But aspell, the printer aspell you don't get access to that directly, you can get access to aspell itself directly. What it does here is that R even has things like spellchecks. Just under prompt in the command line you can simply take take a file and check for its spelling very easily. The [INAUDIBLE] using R through python, and you can simply, you, you get a package called rpi2 [INAUDIBLE]. And you can load it within Python, so the way to do it in Python of course is using the import command. So if you say import rpy2.robjects as robjects, then it becomes accessible to you, and here a simple example of that has been shown. So x equal to robjects.IntVector range ten gives you first the numbers there and r.rnorm. Rnorm is the family r function that we have seen many times, we are simply taking ten numbers out of that and calling x element to plot it later on. So, this functionality of using r through python is also possible, and with normpi you can use it in a very effective manner. I don't know if some of you noticed, but when we printed information about the way these variables in our Galaxy, the input to RA was given to five decimal places. But when we printed it out, then it was only to four decimal places. So was the fifth decimal place lost? Is R not precise? That's certainly not so because, if you see slot of RA here you get only four places, but so where did the last digit vanish? So, this is something about the precision in R. The default precision is at a certain level. But you can also insert R to use a slightly different precision. So you could say that I want to use ten digits, and you can say that by saying options, digit equal, digits equal to ten. And that's again, a suggestion, you can go up to 22. So, when does it get used and when does it not get used? So, look at the next example. Here, we are saying that there is a sprint of pi, and we want it to 100 decimal places, and output is shown there it becomes inaccurate after about 15 decimal places. But does that mean again that R is not precise after about 15 decimal places? No. Again there it depends on whether you are using large numbers or you are sing very small numbers. So the precision can go arbitrary small. Well, not arbitrary small but up to 308 into the R of 308, which is fairly small. And then you can see by seeing what the limit of double is, the Machine$double that is using. And then you can use additional tools within R to go to quite high precision. But then again, when it is that you made into problems, and that is what is shown in the next example. So if you are trying to compare very small numbers. Versus significant digits, then some things that you have to keep in mind are that if the differences are very small, so that the numbers start looking constant and they're not close to zero, then you can be in trouble. So the first example here shows that we are assigning to x1 50 numbers with. A mean of one and a standard deviation that is very small. One minus 15. Right. And why when is where we use something where we say that again we want 50 numbers and in this case, the mean is slightly different with the same standard deviation. So, if you notice, then one and, one and, just a small delta. Now these two numbers are very close to each other and not close to zero. So, there comparison is going to look as if it is a series of constants. And that is going to throw an error for you, because all the numbers seem constant. But in the second example, what we do is that we assign to x2 numbers that are, that have a mean of 0, and again, a very small sign of deviation. And to the second vector, we assign numbers that are very close to zero, mean, but not zero. And the same standard deviation. In this case if you try to compare them you'll be fine. So again, it depends very much on what kind of numbers you are trying to compare and what kind of precision that you want with them. R will allow you to, use them in a proper fashion. Next time we'll be seeing ggplot and some advanced plotting.