Lab01
pdf
keyboard_arrow_up
School
Syracuse University *
*We aren’t endorsed by this school
Course
687
Subject
Mathematics
Date
Feb 20, 2024
Type
Pages
3
Uploaded by DeanTigerMaster997
8/31/23, 11:38 PM
mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=e407d84aa1&attid=0.1&permmsgid=msg-f:177580484565700…
https://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=e407d84aa1&attid=0.1&permmsgid=msg-f:1775804845657004717&th=18a4…
1/3
Intro to Data Science - Lab 1
Copyright 2022, Jeffrey Stanton and Jeffrey Saltz Please do not post online.
Week 1 - Introduction to R
# Enter your name here: Swapnil Deore
Please include nice comments.
Attribution statement: (choose only one and delete the rest)
# 2. I did this lab assignment with help from the book and the professor and these Internet sources:
Each student should run R-Studio on their computer (or via https://rstudio.cloud/
). Remember that R should
always be installed before R-Studio (if you are not using rstudio.cloud or Google Colab)
1. Add together all the numbers between 1 and 10 (inclusive). Take note of the result. Remember, every
student should type and run the code on their machine.
#calling sum function to calculate sum
sum(1:10)
## [1] 55
2. Now create a vector of data that contains the numbers between 1 and 10 (inclusive). Here is a line of code
to do that:
myNumbers <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
#creating vector
myNumbers <- c(1:10)
myNumbers
## [1] 1 2 3 4 5 6 7 8 9 10
3. Now add together all of the numbers that are in the vector myNumbers
. There is a built-in function within
R that can do this for you in one step: Take a guess as to the name of that function and run it on
myNumbers
. Check your result against the results of question 1.
#calling sum function to calculate sum of vector elements
sum(myNumbers)
## [1] 55
4. R can do a powerful operation called ** vector math ** in which a calculation runs on every element of a
vector. Try vector math on myNumbers
by adding 10 to each element of myNumbers, and storing the
result in myNewNumbers
. Print out myNewNumbers
.
#Adding 10 to every element of vector
myNewNumbers = myNumbers + 10
myNewNumbers
## [1] 11 12 13 14 15 16 17 18 19 20
8/31/23, 11:38 PM
mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=e407d84aa1&attid=0.1&permmsgid=msg-f:177580484565700…
https://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=e407d84aa1&attid=0.1&permmsgid=msg-f:1775804845657004717&th=18a4…
2/3
5. Efficiently calculate a sum of the numbers between 11 and 20 (inclusive), using techniques from the
problems above. Hint:
use c(11:20)
sum(c(11:20))
## [1] 155
6. Calculate a sum of all of the numbers between 1 and 100 (inclusive), using techniques from the problems
above.
sum(1:100)
## [1] 5050
7. Make sure you have a variable myNumbers
, that is a vector of 10 numbers (1,2,3,4,5,6,7,8,9,10)
myNumbers
## [1] 1 2 3 4 5 6 7 8 9 10
8. Type the following commands in the code cells below and run each one:
mean(myNumbers)
median(myNumbers)
max(myNumbers)
min(myNumbers)
length(myNumbers)
mean(myNumbers)
## [1] 5.5
median(myNumbers)
## [1] 5.5
max(myNumbers)
## [1] 10
min(myNumbers)
## [1] 1
length(myNumbers)
## [1] 10
9. Repeat the commands from above, this time adding a comment to each line of code in your file explaining
what it does. The comment character is # .
#Function calculates mean
mean(myNumbers)
## [1] 5.5
#Function calculates median
median(myNumbers)
## [1] 5.5
#Function calculates max
max(myNumbers)
8/31/23, 11:38 PM
mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=e407d84aa1&attid=0.1&permmsgid=msg-f:177580484565700…
https://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=e407d84aa1&attid=0.1&permmsgid=msg-f:1775804845657004717&th=18a4…
3/3
## [1] 10
#Function calculates min
min(myNumbers)
## [1] 1
#Function calculates length
length(myNumbers)
## [1] 10
10. In a comment, explain the output of the following command:
myNumbers > 5
#Will provide the boolean values(true or false) based on the > operator
myNumbers > 5
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
11. Explain what in is bigNum
after executing the following command:
bigNum <- myNumbers[myNumbers > 5]
#bigNum will contain values which are greater than 5
bigNum <- myNumbers[myNumbers > 5]
bigNum
## [1] 6 7 8 9 10
12. Whenever you need R to explain what a command does and how it works, use the ?
command or the
help()
command. Add and run these commands:
?mean
help("mean")
?mean
## starting httpd help server ... done
help("mean")
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help