For Task 1, using the R language I created a data frame with descriptive variable names. The variable my.data.frame is also assigned a data frame that contains two columns: Name, with the values "Christiano" and "Ronaldo", and Age, with the respective ages of 45 and 23. The print function is called to display the contents of the data frame in the console, showing how R can organize and represent data. This code illustrates R's abilities to handle data frames and shows naming.
Task 2 involves creating a binary search algorithm! My code implements a simple binary search algorithm to find a target value in a sorted vector. The binary_search function takes a sorted vector and a target value as arguments, initializing the search with two pointers, low and high. A while loop is used to adjust the search space based on comparisons between the middle element and the target value, allowing the function to narrow down potential matches. If the target is found, the index is returned; if not, the function returns -1. This a simple implementation of a binary search algorithm in R!
Task 3 involves showcasing the types of the R language. My program shows the basic built-in types in R, including integers, numeric (double), characters, logical values, and strings. Each variable is first initialized and displayed, showing how to handle different data types. My program also performs operations such as addition, multiplication, subtraction, division, and modulo, illustrating how these operations return appropriate result types based on the operands involved. Additionally, an aggregate type is constructed using a list to group related data.
For the next Task, I implemented a few different control flow statements in R. My program included if-else, switch, for, while, and repeat loops. The program first checks whether a number is positive, negative, or zero using an if-else statement and then shows a switch statement to handle specific cases for that number. Next, a for loop is used to iterate through a sequence and print counts, followed by a while loop that continues printing until a condition is no longer met. Finally, a repeat loop is used to print counts until a break condition is satisfied.
My final R program demonstrates that functions in R are treated as first class objects, meaning that they can be assigned to variables, passed as arguments to other functions, and also executed using references. In the program, the square function is defined to calculate the square of a number. This function is assigned to a variable square_function, showing that functions can indeed be treated as data types.
The program also includes the apply_function, which accepts a function as an argument and executes it with a given value, showing how functions can be passed to other functions. Finally, I show that it's possible to create and use anonymous functions that do not require a name, allowing for some flexibility in R coding.
Compare this with the first Task R code included above. This is how you would create an array utilizing the numpy library in python. Two small arrays are created. Another expression is included which sums the two arrays. In contrast, R code demonstrates basic variable types and mathematical operations directly using R's built-in data structures without requiring an external library like numpy.
My Python code creates a dictionary to represent a data frame with columns for "Name" and "Age", whereas the R code demonstrates the construction of a data frame using the data.frame function to organize similar data into a structured format.
My Python code defines a function that accesses a global variable x and a local variable y to print their sum, while the similar R code demonstrates variable scoping by showing how local and global variables are handled, particularly emphasizing the difference in access levels between function scopes.