Lab-1-2611
.pdf
keyboard_arrow_up
School
Worcester Polytechnic Institute *
*We aren’t endorsed by this school
Course
2611
Subject
Mathematics
Date
May 27, 2024
Type
Pages
4
Uploaded by MinisterInternetSalmon14
MA2611 LAB 1
Part 1: Introducing R 1.
Sources Here we use
R (Statistical software).
• R itself can be found at: http://cran.r-project.org • I also highly recommend the RStudio front end. It makes developing R code much easier. It can be found at: http://www.rstudio.com • Note, RStudio requires that you have R itself already installed (so you have to access both of the web pages above). •
Good place to start: A Step-by-Step Function Guide to Data Analysis by Richard Cotton O'Reilly Media, September 2013 available for free from the library. •
A comprehensive R tutorial: http://cran.r-project.org/doc/manuals/R-intro.html
. 2.
Some Basics To quit R session, use the following command (or simply close the R console window): > q() Here are some illustrations of R’s interactive nature (using simply the command window):
> 5 #you type in a 5 at the prompt and nothing after that. [1] 5 Here note that ‘#’ sign is considered a comment and will not be processed.
2.1 Number Operations > 5 + 4 ### adding two numbers [1] 9 > 5^3 ### will compute 5^3 [1] 125 > pi ### This is a default object. [1] 3.141593 > 10-4 # Subtraction [1] 6 > 3*4 # Multiplication [1] 12 > 5/3 # Division [1] 1.666667
2.2 Vectors To create a vector of numbers or characters, “c( )” command can be used.
> c(1,3,4,7) # To create a vector of numbers: [1] 1 3 4 7 > c("Andrew","Daniel","Rachel","Sarah") # To create a vector of characters: [1] "Andrew" "Daniel" "Rachel" "Sarah" If elements in the vector have a systematic pattern, the following command can also be used. > seq(from=1,to=9,by=1) # values from 1 to 9 increased by 1. [1] 1 2 3 4 5 6 7 8 9 > rep(4, 5) #repeat 4 five times. [1] 4 4 4 4 4 Here note that we can give a name to any object we create as follows. > x <- c(1,3,4,7) # ”<
-
” can also be used instead use “=” sign
> y <- c("Andrew","Daniel","Rachel","Sarah") > temp = c(1,3,4,7) The following commands can be used with the named objects. > ls() # to show the list of all the named objects created. [1] "temp" "x" "y" > rm(temp) # To remove object “tempt” from the list (directory). > ls() [1] "x" "y" > x # to view the vector x: [1] 1 3 4 7 > y [1] "Andrew" "Daniel" "Rachel" "Sarah" > x # to view the vector x: [1] 1 3 4 7 > y [1] "Andrew" "Daniel" "Rachel" "Sarah" > x[2] # View the second element of x: [1] 3 > y[c(1,3)] # View the first and third elements of y, note that “y[1,3]” would fail here.
[1] "Andrew" "Rachel
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