1. Start RStudio. Go to the console and calculate the following

2. Create a new R Markdown file titled “Lab Week 2 [LASTNAME]”, and at the beginning of the file, type the following message

3. Create a chunk and calculate the following in one chunk:

sqrt(16)
## [1] 4
16^0.5
## [1] 4
43 
## [1] 43

4. In the console, type ?log. See what happens to the help window on the lower right side.

5. In the R Markdown file, create a second chunk that contains the following syntax

pi
## [1] 3.141593
round(pi) 
## [1] 3
round(pi, digits=4) 
## [1] 3.1416
trunc(pi) 
## [1] 3

6. In the R Markdown file, create a vector x that contains (3, 6, 8)…

# create x
x <- c(3, 6, 8)

# see the output of x 
print(x)
## [1] 3 6 8
# calculate x/2
x/2
## [1] 1.5 3.0 4.0
#calculate x^2
x^2
## [1]  9 36 64
#calculate sqrt(x)
sqrt(x)
## [1] 1.732051 2.449490 2.828427
#find the second element of x 
x[2]    #The second element of x is 6 
## [1] 6
#find the first and third element of vector x
x[1]    # The first element of x is 3
## [1] 3
x[3]    # The third element of x is 8 
## [1] 8
#generate a vector y that contains values of (2, 5, 1)
y <- c(2, 5, 1)

#print y
print(y)
## [1] 2 5 1
#calculate x-y
x-y
## [1] 1 1 7
#calculate x*y
x*y
## [1]  6 30  8

7. Assume that we have registered the height and weight for four people: Heights in cm are 180, 165, 160, 193; weights in kg are 87, 58, 65, 100. Make two vectors, height and weight, with the data. The body mass index (BMI) is defined as: weight in kg /(height in m)2. Make a vector with the BMI values for the four people.

#Heights in cm are 180, 165, 160, 19
#weights in kg are 87, 58, 65, 100

#Make two (vectors, height and weight, with the data
height<- c(180, 165, 160, 19)
weight<- c(87, 58, 65, 100)


#Convert height to meters since the formular wants height in meters

new_height<- height/100
new_height
## [1] 1.80 1.65 1.60 0.19
#BMI values for the four  people

BMI <- weight/(new_height^2)
BMI 
## [1]   26.85185   21.30395   25.39062 2770.08310

8. Make a vector called score, which contains the following statistics 77, 93, 92, 68,75,100…

#create score
score <- c(77, 93, 92, 68,75,100)

#this calculates the summary of all scores
sum(score) 
## [1] 505
#sorting
sort (score) 
## [1]  68  75  77  92  93 100
#median
median(score) 
## [1] 84.5
#standard deviation
sd(score) 
## [1] 12.54459
#variance
var(score)
## [1] 157.3667
#minimum value
min(score) 
## [1] 68
#maximum value
max(score) 
## [1] 100

9. Load a library called MASS, and load the data within the MASS library called cats…

#load library
library(MASS) 

#read data
data(cats) 


#What’s the dimension of the data “cats”?
dim(cats) # 144  by 3
## [1] 144   3
#How many variables are there? 
ncol(cats) # 3
## [1] 3
#What are they?
colnames(cats) #  "Sex" "Bwt" "Hwt"
## [1] "Sex" "Bwt" "Hwt"
#How many observations are there?
nrow(cats)  # 144
## [1] 144
#Is sex a factor variable? 
is.factor(cats$sex) # False
## [1] FALSE

10. Load a dataset called “Week_2_Data.dta”…

#Load library
library(haven)

#read data 
data<-read_dta('~/Documents/others/Week_2_Data.dta')

#Check if the data set has been imported correctly. 
head(data)
#What’s the dimension of the data “cats”?
dim(data) # 131361 by  5
## [1] 131361      5
#How many variables are there? 
ncol(data) # 5
## [1] 5
#How many observations are there?
nrow(data)  # 131361
## [1] 131361
#What’s the average education (in years) among all respondents? 
mean(data$educ, na.rm = TRUE)
## [1] 13.03642

11. a. Enter the following into a vector with the name color. Remember to surround each piece of text with quotes.

color <- c("purple", "red", "yellow", "brown")
color
## [1] "purple" "red"    "yellow" "brown"
# b. Display the 2nd element in the vector (red) in the console. 
color[2]
## [1] "red"
#c. Enter the following into a vector with the name weight:
weight <- c(23, 21, 18, 26) 
weight
## [1] 23 21 18 26
# d. Join the 2 vectors together using the data.frame function to make a data frame named info with 2 columns and 4 rows. 
info <- data.frame(color,weight)

# e. Display the data frame in the console.
info

12. Save your work and submit your R markdown file (.html) through Canvas (Assignments – Week 2 Lab).