What Is R Programming Language?: Parenthesis Around The Calculation You Want Performed First. This Is Useful
R is a programming language and software environment for statistical analysis and graphics. It allows users to write code to perform tasks like calculating averages, storing values in variables, and creating vectors to hold multiple values. Vectors can be indexed to access specific elements and the class of a vector identifies the data type, like numeric, it contains.
Download as ODT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
66 views
What Is R Programming Language?: Parenthesis Around The Calculation You Want Performed First. This Is Useful
R is a programming language and software environment for statistical analysis and graphics. It allows users to write code to perform tasks like calculating averages, storing values in variables, and creating vectors to hold multiple values. Vectors can be indexed to access specific elements and the class of a vector identifies the data type, like numeric, it contains.
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3
What is R Programming Language?
R is a programming language and free software environment for statistical computing
and graphics that is supported by the R Foundation for Statistical Computing. How to write code in R? print((92 + 87 + 85)/3) ( to find out average) Task-1: The following values are your exam, homework and project scores, respectively in your chemistry class: 90,81,92. Calculate the average score in chemistry class. Task-2: Here are your exam, homework and project scores for writing class, respectively: 84,95,79 Here are your exam, homework and project scores for art class, respectively. 95,86,93 Calculate the average score for each class. Print the results for each calculation. Athematic Operations: Arithmetic operators are used to carry out mathematical operations. For those who are unfamiliar with exponentiation, exponentiation will raise a number to its power, using the ** or ^ operator. Exponentiation is a way of multiplying a number by itself a specific number of times. If we wanted to multiply the value 4 by itself 3 times, this would look like the following using the * operator: 4*4*4 Unfortunately, This quickly gets cumbersome for larger exponents. For example, if we wanted to multiply the value 4 by itself 20 times, we'd need to type out many values. Instead, we can express the calculation as an exponent: 4**20 Running 4**3 will return: [1] 64 A simple way to determine the order of your calculations is to throw a parenthesis around the calculation you want performed first. This is useful for a more complex calculation like this: print( (92 + 87 + 85 + 67 + 92 + 84)/6 - (77 + 90 + 98)/3 ) The R interpreter follows the order of operations rules in mathematics. An easy way to remember this is PEMDAS: Parentheses Exponent Multiplication or Division Addition or Subtraction Task-2: Write a single expression that calculates the average of the following values: 88,87.66667,86,91.33333,84,91,89.33333 On the same expression, subtract this average from your math score 88. Print the entire expression. The R interpreter recognizes comments and treats them as plain text and won't attempt to execute them. There are two main types of comments we can add to our code: inline comment single-line comment inline comment An inline comment is useful whenever we want to annotate, or add more detail to, a specific statement. To add an inline comment at the end of a statement, start with the hash character (#) and then add the comment: print( (92 + 87 + 85)/3 # Finding the math score ) single-line comment A single-line comment spans the full line and is useful when we want to separate our code into sections. To specify that we want a line of text to be treated as a comment, start the line with the hash character (#): # Here, we're finding the average of our scores. Then, subtracting this average from the math score. print( 88 - ((88 + 87.66667 + 86 + 91.33333 + 84 + 91 + 89.33333)/7) )
How to store value in the variable?
chemistry <- 87.66667 print(chemistry) physical_education <-89.33333 print(physical_education) Task-3: Calculate your grade point average using the following variables: o math <- 88 o chemistry <- 87.66667 o writing <- 86 o art <- 91.33333 o history <- 84 o music <- 91 o physical_education <- 89.33333 Store this in gpa. Subtract your gpa from history to see if history is below the average. Store this difference in history_difference. What is Vector in R? In R, we can use a vector to store these values. A vector is a storage container that can store a sequence of values. We can then name a vector using a variable. To create a vector, you'll be using c(). In R, c() is known as a function. Similar to the print() statement, the c() function takes in multiple inputs and stores these values in one place. The c() function doesn't perform any arithmetic operation on the values, it just stores those values. math_chemistry <- c(88,87.66667) We could also create the vector using your variable names as well: math_chemistry <- c(math,chemistry) If we were to print(math_chemistry), it would look like this: [1] 88.00000 87.66667 On the other hand, if we tried to store a sequence of values, like this: math_chemistry <- 88, 87.66667 The R interpreter will only try to assign 88 to math_chemistry but will not be able to interpret the comma after 88: Error: unexpected ',' in "math_chemistry <- 88," Task: 4 Create a vector containing the score of each class using the variable names: math <- 88 chemistry <- 87.66667 writing <- 86 art <- 91.33333 history <- 84 music <- 91 physical_education <- 89.33333 Store this value in final_scores.
How to Print index in R from a vector?
final_scores <- c(88, 87.66667, 86, 91.33333, 84, 91, 89.33333) third<- final_scores[3] print(third) Result: 86 How to find out the vector class that what data type vector contains? Class(final_scores) Result [1] “ numeric ”