| title: “Assignment 2” |
| author: “Ross Ciancio” |
| date: “23/01/2020” |
| output: html_document |
f<-function(N){
N <- 100
print(N*(N+1)/2)
}
N<-sample(1:1,000,000, 1)
f <- function(N){
N <- sample(1:1000000, 1)
print(N*(N+1)/2)
}
sin((exp(1)^(-10))^(.5))
## [1] 0.006737896
G <- function(N){
N <- sample(1:1000000, 1)
print((N*(N+1)/2)^2)
}
log(exp(3))
## [1] 3
log(exp(7))
## [1] 7
log(exp(300))
## [1] 300
#After various calculations, this resulted to be the correct answer. It makes #sense, as the logarithm of e^x is always x.
#Question 2.1
n <- 100
x <- seq(1, n)
sum(x)
## [1] 5050
#The result is 5050. In 1.1, the reuslt was also 5050. This is the equivalent of the sum of the first 100 integers.
#Question 2.2
#The “sum” command is advantegeous because it sums values from a sequence. Our approach in Question 1 was just to assign a value to N. Instead, for this question, we created a list from 1 to N and made R add it up. This can be advntageous when you don’t know the extreme values of a sequence. In that case, the “sum” command can add up all the values present in that sequence without you knowing how many there actually are.
#Question 2.3
x <- seq(from = 0, to = 1, by = 0.1)
#Mean:
(sum(x))/(11)
## [1] 0.5
#Standard Deviation:
((sum((x-0.5)^2)/11)^(.5))
## [1] 0.3162278
#Question 2.4
mean(runif(n=10,min=0,max=1))
## [1] 0.4918936
sd(runif(n=10,min=0,max=1))
## [1] 0.3690243
mean(runif(n=100,min=0,max=1))
## [1] 0.498278
sd(runif(n=100,min=0,max=1))
## [1] 0.2800847
mean(runif(n=1000,min=0,max=1))
## [1] 0.4911496
sd(runif(n=1000,min=0,max=1))
## [1] 0.2906211
mean(runif(n=10000,min=0,max=1))
## [1] 0.4993677
sd(runif(n=10000,min=0,max=1))
## [1] 0.2892717
#Question 3.1
MAT1 = matrix(0, nrow=10, ncol=15)
n <- 1
MAT2= matrix(n, nrow=10, ncol=15)
rnorm(n, mean=2, sd=5)
## [1] 8.361837
#Question 3.2
c ("London", "New York", "Paris", "Shanghai", "New Delhi", "Johannesburg")
## [1] "London" "New York" "Paris" "Shanghai" "New Delhi"
## [6] "Johannesburg"
CityNames <- c ("London", "New York", "Paris", "Shanghai", "New Delhi", "Johannesburg")
class(CityNames)
## [1] "character"
#CityNames is in the class of characters.
length(CityNames)
## [1] 6
#The length is 6
c(21, 30, 26, 34, 38, 29)
## [1] 21 30 26 34 38 29
Temperature <- c(21, 30, 26, 34, 38, 29)
CityTemps1 <- rbind (CityNames, Temperature)
CityTemps2 <- cbind (CityNames, Temperature)
class(CityTemps1)
## [1] "matrix"
class(CityTemps2)
## [1] "matrix"
#The class of each CityTemps object is matrix.
nrow(CityTemps1)
## [1] 2
ncol(CityTemps1)
## [1] 6
#2 rows 6 columns
nrow(CityTemps2)
## [1] 6
ncol(CityTemps2)
## [1] 2
#6 rows 2 columns
#coerce to dataframe
dataframe1 <- as.data.frame(CityTemps1)
dataframe2 <- as.data.frame(CityTemps2)
class(dataframe1)
## [1] "data.frame"
class(dataframe2)
## [1] "data.frame"
#The CityTemps objects are now dataframes, after double-checking with the class command.
#At last, the structure
str(dataframe1)
## 'data.frame': 2 obs. of 6 variables:
## $ V1: Factor w/ 2 levels "21","London": 2 1
## ..- attr(*, "names")= chr "CityNames" "Temperature"
## $ V2: Factor w/ 2 levels "30","New York": 2 1
## ..- attr(*, "names")= chr "CityNames" "Temperature"
## $ V3: Factor w/ 2 levels "26","Paris": 2 1
## ..- attr(*, "names")= chr "CityNames" "Temperature"
## $ V4: Factor w/ 2 levels "34","Shanghai": 2 1
## ..- attr(*, "names")= chr "CityNames" "Temperature"
## $ V5: Factor w/ 2 levels "38","New Delhi": 2 1
## ..- attr(*, "names")= chr "CityNames" "Temperature"
## $ V6: Factor w/ 2 levels "29","Johannesburg": 2 1
## ..- attr(*, "names")= chr "CityNames" "Temperature"
str(dataframe2)
## 'data.frame': 6 obs. of 2 variables:
## $ CityNames : Factor w/ 6 levels "Johannesburg",..: 2 4 5 6 3 1
## $ Temperature: Factor w/ 6 levels "21","26","29",..: 1 4 2 5 6 3