0% found this document useful (0 votes)
101 views

Stat 1261/2260: Principles of Data Science (Fall 2021) Assignment 1: R and Rstudio

This document contains an assignment for a data science course involving the use of R and RStudio. It includes tasks to show and measure the length of different objects, perform operations on vectors like addition and multiplication, create and manipulate matrices and data frames, and calculate values and statistics. The assignment covers basic concepts in R including objects, vectors, matrices, data frames, and functions for calculation, rounding, and finding means.

Uploaded by

Ashu Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Stat 1261/2260: Principles of Data Science (Fall 2021) Assignment 1: R and Rstudio

This document contains an assignment for a data science course involving the use of R and RStudio. It includes tasks to show and measure the length of different objects, perform operations on vectors like addition and multiplication, create and manipulate matrices and data frames, and calculate values and statistics. The assignment covers basic concepts in R including objects, vectors, matrices, data frames, and functions for calculation, rounding, and finding means.

Uploaded by

Ashu Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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)

obj1 <- seq(0,55,5)


obj2 <- c(1,3,5)
obj3 <- c(TRUE, FALSE)
obj4 <- "Data Science”

Answer 1)

###answer 1.1######

obj1 <-seq(0,55,5)

length_of_obj1 <- length(obj1);obj1;length_of_obj1

obj2 <-c(1,3,5);

length_of_obj2 <- length(obj2); obj2; length_of_obj2

obj3 <-c(TRUE, FALSE)

length_of_obj3 <- length(obj3); obj3; length_of_obj3

obj4 <- "Data Science"


length_of_obj4 <- length(obj4); obj4; length_of_obj4

2) What values are returned by the following commands? (9 points)

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 user has typed the following commands into the console.

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)

####### answer 2#####

a <- c(0, 1)

b <- c(TRUE, FALSE)

c <- c("happy", "sad")

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).

result1 <- sqrt 10


result2 <-- "Hello to you!"
3result <- "Hello to you"
result4 <- "Hello to you
result5 <- date()

Answer 3)

#######amswer 3######

#result1 <- sqrt 10

result1 <- sqrt(10)

#result2 <-- "Hello to you!"

result2 <- "Hello to you!"


#3result <- "Hello to you"

result <- "Hello to you"

#result4 <- "Hello to you

result4 <- "Hello to you"

result5 <- date()

4. Matrices (7 points)

1) Create a matrix as follows by writing code (4 points)


## [,1] [,2] [,3] [,4]
## [1,] "a" "d" "g" "j"
## [2,] "b" "e" "h" "k"
## [3,] "c" "f" "i" "l"

## [,1] [,2] [,3] [,4]


## [1,] "a" "b" "c" "d"
## [2,] "e" "f" "g" "h"
## [3,] "i" "j" "k" "l"

2) Extract the value at the 2nd row and 4th column of M1 using R (1 point)

3) Extract the values in the 1st row in M1 using R (1 point)

4) Extract the values in the 2nd column in M2 using R (1 point)


Answer 4)

####answer 4####

alphabeta_small <- letters[seq(1:12)]

M_1 <- matrix(alphabeta_small,3,4); M_1

M_2 <- matrix(alphabeta_small,3,4,byrow=TRUE); M_2

second_row_fourth_column_M_1 <- M_1[2,1]; second_row_fourth_column_M_1

first_row_M_1 <- M_1[1,]; first_row_M_1

second_column_M_2 <- M_2[,2]; second_column_M_2


5. Calculate the following using R (5 points)

1) 𝑙𝑜𝑔10 (5) − 𝑙𝑛(5)

2) 𝑠𝑖𝑛(𝜋/4)+cos(𝜋/3)

3) Round up the result of 2) to the nearest integer

4) Round the result of 1) to two decimal places

5) Calculate the remainder of 200/47 using R

Answer 5)

######answer 5####

cal_1 <- log10(5)-log(5); cal_1

cal_2 <- sin(pi/4)+cos(pi/3); cal_2

cal_integer_round_2 <- round(cal_2); cal_integer_round_1

cal_integer_round_1 <- round(cal_1,2); cal_integer_round_2

cal_3 <- 200 %% 47; cal_3


6. Data Frames (15 points)

1) Create a data frame as follows (4 points):


## Name Year GPA
## 1 Jacob 1 3.41
## 2 James 2 3.53
## 3 Jeremy 2 3.32
## 4 Jimmy 4 3.67
## 5 John 3 4.00

2) Use column name to extract the names of students. (2 points)

3) Select the information about James (2 points).

4) List name(s) of students whose GPA is higher than 3.5 (3 points).

5) List the GPA of all 2nd-year students and calculate the mean GPA. (4 points)

Answer 6)

####answer 6#####

Name <- c("Jacob", "James", "Jeremy", "Jimmy", "John")

Year <- c(1,2,2,4,3)

GPA <- c(3.41,3.53, 3.32, 3.67, 4.00)

data <- data.frame(Name, Year, GPA);data

name_of_students <- data$Name; name_of_students

student_info <- data[2,]; student_info

data_greater_than_gpa_three_point_five <- data[GPA > 3.5,]$Name;


data_greater_than_gpa_three_point_five

student_gpa_in_second_year <- data[Year == 2,]$GPA; student_gpa_in_second_year

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

You might also like