Stat 1261/2260: Principles of Data Science (Fall 2021) Assignment 1: R and Rstudio
Stat 1261/2260: Principles of Data Science (Fall 2021) Assignment 1: R and Rstudio
1) Show the value of each object and use an R function to measure the length of them. (4 points)
Answer 1)
###answer 1.1######
obj1 <-seq(0,55,5)
obj2 <-c(1,3,5);
obj1 * (-2)
obj1[1:5]
obj1[-2]
obj1 + obj2
obj1 * obj3
obj1 + obj4
obj2 + obj3
sum(obj2)
sum(obj3)
Answer 2)
###answer 1.2######
ans_1<-obj1*-2;ans_1
ans_2<-obj1[1:5];ans_2
ans_3<-obj1[-2];ans_3
ans_4<-obj1+obj2;ans_4
ans_5<-obj1*obj3;ans_5
ans_6<-c(obj1,obj4);ans_6
ans_7<-obj2+obj3;ans_7
ans_8<-sum(obj2);ans_8
ans_9<-sum(obj3);ans_9
2. Vectors (5 points)
a <- c(0, 1)
b <- c(TRUE, FALSE)
c <- c("happy", "sad")
What do each of the following commands return? Pay attention to the type of the vector after
concatenating two vectors.
a+b
a*b
d <- c(a,b);d
e <- c(a,c);e
g <- c(b,c);g
Answer 2)
a <- c(0, 1)
a+b
a*b
d <- c(a,b);d
e <- c(a,c);e
g <- c(b,c);g
mode(a+b)
mode(a*b)
mode(d)
mode(e)
mode(g)
3. Errors (5 points)
For each of the following assignment statements, describe the error (or note why it does not generate
an error).
Answer 3)
#######amswer 3######
4. Matrices (7 points)
2) Extract the value at the 2nd row and 4th column of M1 using R (1 point)
####answer 4####
2) 𝑠𝑖𝑛(𝜋/4)+cos(𝜋/3)
Answer 5)
######answer 5####
5) List the GPA of all 2nd-year students and calculate the mean GPA. (4 points)
Answer 6)
####answer 6#####
mean_gpa_of_second_year = mean(student_gpa_in_second_year);mean_gpa_of_second_year
mean_gpa_of_all_students =mean(data$GPA);mean_gpa_of_all_students