## Create variable `my_name` that is equal to your first name and print it
my_name <- "Ott"
cat("I am", my_name, "\n")
## I am Ott
## Sometimes you may use "print" instead, this prints just the value:
print(my_name)
## [1] "Ott"
library(stringr)
## 1.1.1
## Create a numeric variable \texttt{my\_age} that is equal to your age
my_age <- 18
my_age
## [1] 18
## 1.1.2
## How many seconds is there in a year? Compute this number!
## Assign it to a suitably named variable and print the result with a
## informative message
sec <- 86400
days <- 365
total <- sec * days
str_c("there are ", total," sec in a year")
## [1] "there are 31536000 sec in a year"
## 1.1.3
## What is your age in seconds? Use the variable \texttt{my\_age}
## you computed above and seconds-in-year, and compute it. Assign the result to a
## suitably named variable and print it.
str_c("I am ", my_age * days * sec, " seconds old")
## [1] "I am 567648000 seconds old"
# 1.1.4
## How many seconds is a typical human lifetime?
ave_life <- 73.5
ave_sec <- ave_life * sec
str_c("there are ",ave_sec, " sec in a typical human lifetime")
## [1] "there are 6350400 sec in a typical human lifetime"
#1.1.2
c <- 300000000
m0 <- 60
v <- 10
gma = sqrt(1 - (v**2)/(c**2))
m0
## [1] 60
#1.1.2
m = m0 / gma
diff = m - m0
diff
## [1] 3.552714e-14
#1.1.3
m01 <- 1.672 * 10**(-27)
v1 <- 270000000
gma1 = sqrt(1 - (v1**2)/(c**2))
m1 = m01 / gma1
diff1 = m1 - m01
diff1
## [1] 2.163831e-27
#1.1.4
porpotion = diff/diff1
porpotion
## [1] 1.641863e+13
#2.1
I_am_old <- my_age * days * sec > 650000000
str_c(" it is ", I_am_old," I am over 650M sec old")
## [1] " it is FALSE I am over 650M sec old"
#2.2
older <- 1000000000 < my_age* days * sec
old <- 650000000 > my_age* days * sec
str_c(" it is ", older," that I am less then 1B sec, but it is ",old," more then 650M sec old")
## [1] " it is FALSE that I am less then 1B sec, but it is TRUE more then 650M sec old"
#2.3
logical_expression <- my_age * days * sec > 500000000& !(my_age* days * sec > 700000000)
print(logical_expression)
## [1] TRUE
#3.1
library(stringr)
#3.2
name <- "Isabel"
template <- "Hello, my name is name, and I’m age years old."
#3.3
name <- "Isabel"
age <- 18
#3.4
intro <- str_replace_all(template, c("name" = name, "age" = as.character(age)))
#3.5
loud_intro <- toupper(intro)
cat("Loud Intro:", loud_intro, "\n")
## Loud Intro: HELLO, MY ISABEL IS ISABEL, AND I’M 18 YEARS OLD.
#3.6
e_count <- str_count(loud_intro, "E")
cat("Number of 'E' in loud_intro:", e_count, "\n")
## Number of 'E' in loud_intro: 4
#4.1
for (i in 1:10) {
cat(i, "\n")
}
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9
## 10
#4.2
for (i in 1:10) {
square <- i^2
cat("The square of", i, "is", square, "\n")
}
## The square of 1 is 1
## The square of 2 is 4
## The square of 3 is 9
## The square of 4 is 16
## The square of 5 is 25
## The square of 6 is 36
## The square of 7 is 49
## The square of 8 is 64
## The square of 9 is 81
## The square of 10 is 100
#4.3
for (i in 1:10) {
square <- i^2
cat(i, "^2 =", square, "\n", sep = " ")
}
## 1 ^2 = 1
## 2 ^2 = 4
## 3 ^2 = 9
## 4 ^2 = 16
## 5 ^2 = 25
## 6 ^2 = 36
## 7 ^2 = 49
## 8 ^2 = 64
## 9 ^2 = 81
## 10 ^2 = 100
# 4.4
total_sum <- 0
for (i in 1:100) {
total_sum <- total_sum + i
}
cat("The sum of numbers from 1 to 100 is:", total_sum, "\n")
## The sum of numbers from 1 to 100 is: 5050
#4.5
# Use nested loops to compute and print factorials from 10! down to 1!
for (n in 10:1) {
factorial_result <- 1
for (i in 1:n) {
factorial_result <- factorial_result * i
}
cat(n, "! =", factorial_result, "\n")
}
## 10 ! = 3628800
## 9 ! = 362880
## 8 ! = 40320
## 7 ! = 5040
## 6 ! = 720
## 5 ! = 120
## 4 ! = 24
## 3 ! = 6
## 2 ! = 2
## 1 ! = 1