# Creating a variable called "age" and assigning it the value 25
<- 25
age
# Creating a variable called "name" and assigning it a string value
<- "John Doe"
name
# Creating a variable called "score" and assigning it the value 90
<- 90
score
# Creating a vector of numbers and assigning it to a variable called "grades"
<- c(80, 90, 85, 92, 87) grades
2 Variables, expressions, and statements
Variables, expressions, and statements are fundamental concepts in programming, including in R and Python. In both languages, a variable is a named container for data, and it can hold different types of data such as integers, floats, and strings. An expression is a combination of variables, operators, and functions that evaluates to a value. Expressions are commonly used to perform mathematical operations, logical comparisons, and data manipulation. In R, statements are executed line-by-line in a script or console, and can include function calls, variable assignments, and control flow structures such as loops and conditionals. Python is similar in that statements can include function calls and variable assignments, but also includes additional constructs such as classes and modules that allow for more advanced programming concepts. Understanding variables, expressions, and statements is essential for developing proficiency in R and Python programming, and lays the foundation for more complex programming tasks.
In terms of differences between the two languages, one notable difference is the use of assignment operators. In R, the assignment operator is <-, while in Python it is =. Additionally, Python requires indentation to denote code blocks, whereas R uses curly braces. Another difference is that R has built-in support for statistical analysis and data manipulation, while Python is often used more broadly for tasks such as web development and machine learning. Overall, both languages have their strengths and weaknesses, and the choice between the two often depends on the specific needs of the project or task at hand.
2.1 Basic Exmaple
Here are some examples in both R and Python:
2.1.1 Variables
2.1.1.1 R example
2.1.1.2 Python example
The ‘python’ engine in knitr requires the reticulate
package.
library(reticulate)
# Creating a variable called "age" and assigning it the value 25
= 25
age
# Creating a variable called "name" and assigning it a string value
= "John Doe"
name
# Creating a variable called "score" and assigning it the value 90
= 90
score
# Creating a list of numbers and assigning it to a variable called "grades"
= [80, 90, 85, 92, 87] grades
2.1.2 Expressions
2.1.2.1 R example
# Performing a mathematical expression and storing the result in a variable called "result"
<- 5 + 10
result
# Combining variables and strings in an expression to create a new string
<- paste("Hello", name, "your age is", age)
greeting
# Using a logical expression to check if a variable is greater than 10
<- score > 10
is_greater_than_10
# Combining variables, functions, and operators in an expression to create a new vector
<- grades * 0.7 + 30 new_grades
2.1.2.2 Python example
# Performing a mathematical expression and storing the result in a variable called "result"
= 5 + 10
result
# Combining variables and strings in an expression to create a new string
= "Hello " + name + ", your age is " + str(age)
greeting
# Using a logical expression to check if a variable is greater than 10
= score > 10
is_greater_than_10
# Combining variables, functions, and operators in an expression to create a new list
= [x * 0.7 + 30 for x in grades] new_grades
2.1.3 Statements
2.1.3.1 R example
# Assigning a value to a variable and then printing it
<- 25
age print(age)
[1] 25
# Using an if statement to control program flow
if (age > 18) {
print("You are an adult")
else {
} print("You are a minor")
}
[1] "You are an adult"
# Using a for loop to iterate over a vector and print each value
for (grade in grades) {
print(grade)
}
[1] 80
[1] 90
[1] 85
[1] 92
[1] 87
# Defining a function and calling it with arguments
<- function(score) {
calculate_grade if (score >= 90) {
return("A")
else if (score >= 80) {
} return("B")
else {
} return("C")
}
}
<- calculate_grade(score)
grade print(grade)
[1] "A"
2.1.3.2 Python example
# Assigning a value to a variable and then printing it
= 25
age print(age)
# Using an if statement to control program flow
25
if age > 18:
print("You are an adult")
else:
print("You are a minor")
# Using a for loop to iterate over a list and print each value
You are an adult
for grade in grades:
print(grade)
# Defining a function and calling it with arguments
80
90
85
92
87
def calculate_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
else:
return "C"
= calculate_grade(score)
grade print(grade)
A