Page 188 - Data Science Algorithms in a Week
P. 188

R Reference


                rectangle_side_a, "and", rectangle_side_b, "cm.\n")
                cat("Then the area of the rectangle is", rectangle_area, "cm squared.\n")
                cat("The perimeter of the rectangle is", rectangle_perimeter, "cm.\n")
            Output:

                $ Rscript example02_int.r
                Let there be a rectangle with the sides of lengths: 10 and 5 cm.
                Then the area of the rectangle is 50 cm squared.
                The perimeter of the rectangle is 30 cm.


            Numeric

            The numeric data type can also hold non-integer rational values.

            Input:

                source_code/appendix_b_r/example03_numeric.r
                pi = 3.14159
                circle_radius = 10.2
                circle_perimeter = 2 * pi * circle_radius
                circle_area = pi * circle_radius * circle_radius
                cat("Let there be a circle with the radius", circle_radius, "cm.\n")
                cat("Then the perimeter of the circle is", circle_perimeter, "cm.\n")
                cat("The area of the circle is", circle_area, "cm squared.\n")

            Output:
                $ Rscript example03_numeric.r
                Let there be a circle with the radius 10.2 cm.
                Then the perimeter of the circle is 64.08844 cm.
                The area of the circle is 326.851 cm squared.



            String

            A string variable can be used to store text.
            Input:

                source_code/appendix_b_r/example04_string.r
                first_name = "Satoshi"
                last_name = "Nakamoto"
                #String concatenation is performed with the command paste.
                full_name = paste(first_name, last_name, sep = " ", collapse = NULL)
                cat("The invertor of Bitcoin is", full_name, ".\n")


                                                    [ 176 ]
   183   184   185   186   187   188   189   190   191   192   193