## 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"
## 1.1.1
## Create a numeric variable \texttt{my\_age} that is equal to your age
my_age <- 18
## 1.1.2
## How many seconds is there in a year? Compute this number!
365*24*60*60
## [1] 31536000
## Assign it to a suitably named variable and print the result with a
seconds_in_year <- 31536000
## informative message
cat("There are", seconds_in_year, "seconds in a year.","\n")
## There are 31536000 seconds 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.
my_age_in_seconds <- my_age * seconds_in_year
cat("My age in seconds is", my_age_in_seconds, "seconds")
## My age in seconds is 567648000 seconds
## 1.1.4
## How many seconds is a typical human lifetime?
lifetime_in_seconds = 73.5 * seconds_in_year
cat("A typical human lifetime is", lifetime_in_seconds, "seconds")
## A typical human lifetime is 2317896000 seconds
## The rest of the questions are in the pdf. You do not need to copy the questions
## here, but please add the question numbers!
## Good luck!
## 1.2.1
## the moving mass of a student who weighs m0=60kg and running 10m/s
student_rest_mass <- 60
student_moving_mass <- student_rest_mass/(sqrt(1-(10^2/(3e8)^2)))
cat(student_moving_mass,"kg")
## 60 kg
## 1.2.2
student_moving_mass-student_rest_mass
## [1] 3.552714e-14
## 1.2.3
proton_rest_mass <- 1.672e-27
proton_moving_mass <- proton_rest_mass /(sqrt(1-(270000000^2/(3e8)^2)))
## 1.3.4
proton_moving_mass/proton_rest_mass
## [1] 2.294157
## 2.1
I_am_old <- my_age > 650*1000000/60/60/24/360
cat("Am I old? ", I_am_old, "!", sep = "")
## Am I old? FALSE!
## 2.2
my_age > 650*1000000/60/60/24/360 & my_age < 1*1000000000/60/60/24/360
## [1] FALSE
## 2.3
my_age > 500*1000000/60/60/24/360 & !(my_age>700*1000000/60/60/24/360)
## [1] TRUE
## 3.1
library(stringr)
## 3.2
template <- "Hello, my name is {name}, and I'm {age} years old."
## 3.3
name <- "Meiyao"
age <- 18
## 3.4
age <- paste(age,sep="")
intro_name <- str_replace(template, "\\{name\\}",name)
intro <- str_replace(intro_name,"\\{age\\}", age)
## 3.5
loud_intro <- toupper(intro)
loud_intro
## [1] "HELLO, MY NAME IS MEIYAO, AND I'M 18 YEARS OLD."
## 3.6
str_count(intro,pattern="e")
## [1] 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){
cat(i^2,"\n")
}
## 1
## 4
## 9
## 16
## 25
## 36
## 49
## 64
## 81
## 100
## 4.3
for(i in 1:10){
cat(i,"^2 = ",i^2, "\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
sum <- 0
for (i in 1:100){
sum <- sum + i
}
cat("The sum of all numbers from 1 to 100 is",sum)
## The sum of all numbers from 1 to 100 is 5050
## 4.5
for (i in 10:1){
factorial <- 1
for (j in i:1){
factorial <- factorial * j
}
cat(i,"! = ", factorial, sep="","\n")
}
## 10! = 3628800
## 9! = 362880
## 8! = 40320
## 7! = 5040
## 6! = 720
## 5! = 120
## 4! = 24
## 3! = 6
## 2! = 2
## 1! = 1