Ex 1
Ex 1
python
CopyEdit
import numpy as np
y1 = np.random.normal(size=n)
print(y1)
Explanation:
(2b) Create another n-item vector that has n added to each random uniform distribution
python
CopyEdit
y2 = np.random.uniform(size=n) + n
print(y2)
Explanation:
python
CopyEdit
del y1
Explanation:
python
CopyEdit
globals().clear()
Explanation:
• globals().clear() removes all user-defined variables.
python
CopyEdit
import math
print(math.sqrt(16))
Explanation:
R Commands
CopyEdit
x <- runif(10)
print(x)
CopyEdit
plot(x)
CopyEdit
CopyEdit
sample(months, 2)
(3e) Combine two strings with comma separation
CopyEdit
CopyEdit
CopyEdit
nchar("HelloWorld")
CopyEdit
print(odd_numbers)
CopyEdit
log10(odd_numbers)
sin(odd_numbers)
(3j) Compute sum, mean, median, length, variance, standard deviation, and quantiles
CopyEdit
sum(odd_numbers)
mean(odd_numbers)
median(odd_numbers)
length(odd_numbers)
var(odd_numbers)
sd(odd_numbers)
quantile(odd_numbers)
CopyEdit
summary(odd_numbers)
CopyEdit
Sys.Date()
Sys.time()
CopyEdit
as.numeric(Sys.Date() - as.Date("YYYY-MM-DD"))
CopyEdit
tolower("Hello World")
toupper("Hello World")
CopyEdit
CopyEdit
CopyEdit
CopyEdit
sum(grepl("^t", tolower(words)))
CopyEdit
sum(grepl("[aeiou]$", tolower(words)))
(3t) Find all words containing at least one vowel and negate
CopyEdit
words[!grepl("[aeiou]", words)]
CopyEdit
words[grepl("^[^aeiou]+$", words)]
CopyEdit
sort(words)
Handling NA and Inf Values in R
CopyEdit
mean(x, na.rm=TRUE)
median(x, na.rm=TRUE)
mean(y, na.rm=TRUE)
median(y, na.rm=TRUE)
CopyEdit
mean(x, na.rm=TRUE)
median(x, na.rm=TRUE)
CopyEdit
length(NULL) # Output: 0
(4d) Construct a list of a numeric vector, logical, string, and another list
CopyEdit
print(my_list)
CopyEdit
names(my_list) <- c("num_vector", "logic", "str", "nested_list")
names(my_list)
(4f) Create a matrix using matrix() and diag() and perform operations
CopyEdit
# Matrix operations
t(A) # Transpose
det(A) # Determinant
CopyEdit
print(df)