# Untitled unknown
r
a month ago
5.7 kB
2
Indexable
Never
```# Define a numeric vector 'x'
x = c(2, 3, 2, 5, 8)

# Create a list called 'structure' to store information about 'x'
structure = list(
mean = mean(x),
minimum = min(x),
length = length(x)
)

# Display the 'structure' list
print(structure)

# Access and print the 'mean' value from the 'structure' using \$
print(structure\$mean)

# Access and print the third element from the 'structure' using [[ ]]
print(structure[])

# Create a named vector 's1'
s1 = c(name = "Jan", age = 25, student = TRUE)

# Display the attributes of 's1'
attributes(s1)

# Access and print the 'name' element from 's1'
s1["name"]

# Create a 2x4 array 't1' filled with zeros
(t1 = array(0, c(2, 4)))

# Create a 2x3 array 't2' with values from 1 to 6
(t2 = array(1:6, c(2, 3)))

# Display the dimensions of 't2'
dim(t2)

# Access and print the element in the first row and third column of 't2'
t2[1, 3]

# Access and print the first row of 't2'
t2[1, ]

# Access and print the third column of 't2'
t2[, 3]

# Create a matrix with values from 1 to 6, specifying rows and columns
matrix(1:6, nrow = 2, ncol = 3)

# Combine columns using cbind
cbind(c(1, 2), c(3, 4), c(5, 6))

# Combine rows using rbind
rbind(c(1, 3, 5), c(2, 4, 6))

# Create a matrix 'A' with values from 1 to 6, filling by row
(A = matrix(1:6, 2, 3, byrow = TRUE))

# Get the number of rows in matrix 'A'
nrow(A)

# Get the number of columns in matrix 'A'
ncol(A)

# Create another matrix 'B'
(B = matrix(2:7, 3, 2))

# Multiply matrices 'A' and 'B'
A %*% B
B %*% A

# Calculate the scalar product of two vectors
c(1, 2, 3) %*% c(3, 4, 5)

# Create a 3x3 matrix 'D'
(D = matrix(c(1, 2, 2, 3, 5, 2, 3, 1, 4), 3, 3))

# Transpose matrix 'D'
t(D)

# Calculate the determinant of matrix 'D'
det(D)

# Find the inverse of matrix 'D'
solve(D)

# Create a 3x3 matrix 'C'
(C = matrix(1:9, 3, 3))

# Attempt to find the inverse of matrix 'C'
solve(C)

# Calculate the determinant of matrix 'C'
det(C)

# Solve the equation DX=C for X, if a unique solution exists
solve(D, C)

# Calculate the eigenvalues and eigenvectors of matrix 'D'
eigen(D)

# Get the values on the diagonal of matrix 'D'
diag(D)

# Create a data frame 'data' with columns 'name', 'animal', and 'age'
data = data.frame(c("Rex", "Kitty"), c("dog", "cat"), c(5, 4))

# Assign column names to 'data'
names(data) = c("name", "animal", "age")

# Display the 'data' frame
data

# Access and print the 'name' column from 'data'
data\$name

# Display the attributes of 'data'
attributes(data)

# Access and print the first column of 'data'
data

# Access and print the 'name' column using column index
data[, 1]

# Define two variables 'x' and 'y'
x = "4"
y = "5"

# Add 'x' and 'y' as character strings
x + y

# Convert 'x' and 'y' to numeric and then add them
as.numeric(x) + as.numeric(y)

# Create a character vector 'letters'
letters = c("a", "b", "c", "a", "c")

# Convert 'letters' into a factor 'l2'
(l2 = factor(letters))

# Convert 'l2' back to a character vector
as.vector(l2)

# Attempt to convert 'l2' to numeric (using levels as numbers)
as.numeric(l2)

# Create a diagonal matrix 'E' with diagonal elements 1, 2, and 3
(E = diag(c(1, 2, 3)))

# Convert 'E' into a vector
as.vector(E)

# Modify the values of 'E' manually
fix(E)

# Display the modified 'E' matrix
E

# Convert 'E' into a vector again
as.vector(E)

# Define a function 'sq' to calculate the square of a number plus 1
sq = function(x) x^2 + 1

# Calculate the square of 2 plus 1 using the 'sq' function
sq(2)

# Define a function 'f' to locally change a variable 'x'
f = function(x) {
x = 2 * x + 5
x
}

# Call the 'f' function with an argument of 2
f(2)

# Check the value of 'x' after calling the 'f' function
x

# Assign a value of 5 to the variable 'x'
x = 5

# Use conditional statements to check the value of 'x'
if (x < 5) {
print("x < 5")
} else if (x == 5) {
print("x = 5")
} else {
print("x > 5")
}

# Define a function 'g' to print a message based on the value of 'x'
g = function(x) {
if (x < 5) {
cat(x, "<5\n")
} else if (x == 5) {
cat(x, "=5\n")
} else {
cat(x, ">5\n")
}
}

# Call the 'g' function with different values of 'x'
g(3)
g(5)
g(7)

# Use a for loop to count down from 9 to 0
for (i in 9:0) {
cat(i)
cat("...\n")
}

# Use a for loop to calculate the sum of numbers from 1 to 8
z = 0
for (i in 8:1) {
z = z + i
cat(i, "+ ")
}
cat(0, "=", z, "\n")

# Initialize a variable 'i' and use a while
# Initialize a variable 'i' and use a while loop to increment it until it reaches 10
i = 1
while (i < 10) {
i = i + 1
}

# Check the value of 'i' after the while loop
i

# Define a function 'odd' to calculate the highest power of 2 that divides 'x'
odd = function(x) {
i = 0
while (x %% 2 == 0) {
i = i + 1
x = x / 2
}
cat(x * 2^i, "=", x, "*", 2^i, "\n")
}

# Call the 'odd' function with an argument of 864
odd(864)

# Exercise 9: Solve the system of equations
# 5x + 2y - z = 4
# x - 3y - 2z = -1
# 3x + 2y + 3z = 5

# Exercise 10: Calculate the mean of petal width for each species and jointly from the 'iris' dataset
iris

# Exercise 11: Find the greatest natural number 'n' such that the sum of square roots of natural numbers
# less than or equal to 'n' does not exceed 300

# Exercise 12: Create a vector 'X' with numbers from 1 to 100 in random order and create a vector 'Y'
# where each element is 1 if the corresponding element in 'X' is greater than its position

# Exercise 13: Define a function that calculates the number of ones in a vector

# Note: The code for Exercises 9 to 13 is not provided in the original snippet.
```