R introduction
Introduction to R
Spring 2023
Prof. Dr. Asad Ali
Department of Applied Mathematics & Statistics
Institute of Space Technology
Islamabad, Pakistan
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Overview
R Mechanics
Resources for Getting Help
Vectors
Rectangular Data
Plotting and Graphics
Control Structures, Looping, and Applying
Functions
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
What is R?
A software package
A programming language
A toolkit for developing statistical and analytical tools
An extensive library of statistical and mathematical software and algorithms
A scripting language
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Why R?
R is cross-platform. Write one code and run it on Windows, Mac, and Linux (as
well as more complicated systems) without any modifications.
R provides a large number of useful statistical tools, many of which have been
painstakingly tested. Millions of people are contributing their packages to
R community for free.
R produces publication-quality graphics in a variety of formats.
R plays well with FORTRAN, C, and scripts in many languages.
R scales, making it useful for small and large projects. It is NOT Excel.
R avoids the GUI. Mouse is an annoying animal!
Supercomputing
One can develop code for analysis on his laptop. Can then deploy the same code on a
20000 core cluster and run it in parallel on 100 samples, monitor the process, and then
update a database with R when complete.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R License and the Open Source Ideal
R is free!
Distributed under GNU license
You may download the source code.
You may modify the source code to your heart’s content.
You may distribute the modified source code and even charge money for it, but
you must distribute the modified source code under the original GNU license
Take-home Message
This license means that R will always be available, will always be open source, and can
grow organically without constraint.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
A typical GNU license
When you start R, you see
The underlined paragraph is the GNU license.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Downloading and installing R
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Starting R
Depends on operating system and interface
In windows one simply click on R icon on desktop or select from Start menu.
Linux command line is $ R.
The R files have an extension of ‘R‘ or ‘r‘ e.g. file.R.
Every programming language comes with one or more script editors called
integrated development environment (IDE).
Today, the most powerful IDE for R is RStudio.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Getting started
R Commands are either:
1 Assignments
> x = 1
> y <- 2
2 Expressions
> 1 + pi + sin(3.7)
[1] 3.611757
The “<-“ and “=“ are both assignment operators. The standard R prompt is a
“>“ sign. If a line is not a complete R command, R will continue the next line
with a “+“.
> 1 + pi +
+ sin(3.7)
[1] 3.611757
Similarly if a parentheses or bracket is incomplete, R will keep adding a “+“ sign
at each new line. It‘s completely normal.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Rules for Names in R
Any combination of letters, numbers, underscore, and “.“
May not start with numbers, underscore.
R is case-sensitive.
Examples
pi
x
camelCaps
my stuff
MY Stuff
this.is.the.name.of.the.man
ABC123
abc1234asdf
.hi
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Help Functions
If you know the name of the function or object on which you want help:
> help(mean)
> help(‘mean‘)
> ?mean
It will not only tell you what is it for, but also will give a few numerical examples.
If you do not know the name of the function or object on which you want help:
> help.search(‘microarray‘)
> RSiteSearch(‘microarray‘)
Or search by keywords on RSiteSearch http://finzi.psych.upenn.edu/search.html
Many online resources which you will collect over the space of the course
Using Help
I strongly recommend using help(newfunction) for all functions that are new or
unfamiliar to you.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Basic R Operations and Concepts
1Arithmetic
> 2 + 3 # add
[1] 5
> 4 * 5 / 6 # multiply and divide
[1] 3.333333
> 7^8 # 7 to the 8 th power
[1] 5764801
Notice the comment character #. Anything typed after a # symbol is ignored by R.
We know that 20/6 is a repeating decimal, but the above example shows only 7 digits.
We can change the number of digits displayed with options:
> options ( digits = 16)
> 10 / 3 # see more digits
[1] 3 .33333333333 3333
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Basic R Operations and Concepts
> sqrt (2) # square root
[1] 1 .41421356237 3095
> exp (1) # Euler ‘ s constant , e
[1] 2 .71828182845 9045
> sin (30 * pi / 180) # trigonometric functions
[1] 0.5
> pi # Pi
[1] 3 .14159265358 9793
> options ( digits = 7) # back to default
2 Vectors
In R, even a single value is a vector with length=1.
> z = 1
> z
[1] 1
> length ( z )
[1] 1
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Basic R Operations and Concepts
Vectors can contain numbers, strings (character data), or logical values (TRUE
and FALSE)
Vectors cannot contain a mix of types!
Character Vectors
Character vectors are entered with each value surrounded by single or double quotes;
either is acceptable, but they must match. They are always displayed by R with double
quotes.
> # examples of vectors
> c ( ‘ hello ’ ,‘ world ’)
[1] " hello " " world "
> c (1 , 3 , 4 , 5 , 1 , 2)
[1] 1 3 4 5 1 2
> c (1.12341 e7 , 78234.126)
[1] 11234100.00 78234.13
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Regular Sequences
> # create a vector of integers from 1 to 10
> x = 1:10
> x
[1] 1 2 3 4 5 6 7 8 9 10
> # and backwards
> x = 10:1
> x
[1] 10 9 8 7 6 5 4 3 2 1
> # create a vector of numbers from 1 to 4 skipping by 0.3
> y = seq (1 ,4 ,0.3)
> # create a sequence by concatenating two other sequences
> z = c (y , x )
[1] 1.0 1.3 1.6 1.9 2.2 2.5 2.8 3.1 3.4 3.7 4.0 10.0
[13] 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Vector Operations
Operations on a single vector are typically done element-by-element
If the operation involves two vectors:
Same length: R simply applies the operation to each pair of elements.
Different lengths, but one length a multiple of the other: R reuses the shorter vector
as needed
Different lengths, but one length not a multiple of the other: R reuses the shorter
vector as needed and delivers a warning
Typical operations include multiplication (“*”), addition, subtraction, division,
exponentiation (“ˆ” but many operations in R operate on “ ”), vectors and are
then called “vectorized”.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
> x = 1:10
> x+x
[1] 2 4 6 8 10 12 14 16 18 20
> y = 7
> x * y
[1] 7 14 21 28 35 42 49 56 63 70
> y = c (1 ,2 ,3)
> z = x * y
> length ( z )
[1] 10
> z
[1] 1 4 9 4 10 18 7 16 27 10
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Logical Vectors
Logical vectors are vectors composed on only the values TRUE and FALSE. Note the
all-upper-case and no quotation marks.
> a = c ( TRUE , FALSE , TRUE )
> # we can also create a logical vector from a numeric vector 0
> b = c (1 ,0 ,217)
> d = as . logical ( b )
> d
[1] TRUE FALSE TRUE
> # test if a and d are the same at every element
> all . equal (a , d )
[1] TRUE
> # We can also convert from logical to numeric
> as . numeric ( a )
[1] 1 0 1
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Logical Operators
Some operators like <, >, ==, >=, <=, ! = can be used to create logical vectors.
> x = 1:10 # create a numeric vector
> # testing whether x > 5 creates a logical vector
> x > 5
[1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
TRUE
> x <= 5
[1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
> x != 5
[1] TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
> x == 5
[1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
> # we can also assign the results to a variable
> y = ( x == 5)
> y
[1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Indexing Vectors
In programming, an index is used to refer to a specific element or set of elements in an
vector (or other data structure). R uses [ ] to perform indexing.
> x = seq (0 ,1 ,0.1)
> # create a new vector from the 4 th element of x
> x [4]
[1] 0.3
Indexing can use other vectors for the indexing
> x [ c (3 ,4 ,5 ,6)]
[1] 0.2 0.3 0.4 0.5 # OR
> x [3:6]
[1] 0.2 0.3 0.4 0.5 # OR
> y = 3:6
> x[y]
[1] 0.2 0.3 0.4 0.5 # All the three approaches are equivalent .
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Concatenating Strings
R uses the paste function to concatenate strings.
> paste ( " abc " ," def " )
[1] " abc ␣ def "
> paste ( " abc " ," def " , sep = " THISSEP " )
[1] " abcTHISSEPdef "
> paste0 ( " abc " ," def " )
[1] " abcdef "
> paste ( c ( " X " ," Y " ) ,1:10)
[1] " X ␣ 1 " " Y ␣ 2 " " X ␣ 3 " " Y ␣ 4 " " X ␣ 5 " " Y ␣ 6 " " X ␣ 7 " " Y ␣ 8 " " X ␣ 9 " " Y ␣ 10 "
> paste ( c ( " X " ," Y " ) ,1:10 , sep = " _ " )
[1] " X _ 1 " " Y _ 2 " " X _ 3 " " Y _ 4 " " X _ 5 " " Y _ 6 " " X _ 7 "
" Y _ 8 " " X _ 9 " " Y _ 10 "
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Matrices and Data Frames
A matrix is a rectangular array. It can be viewed as a collection of column vectors
all of the same length and the same type (i.e. numeric, character or logical).
A data frame is also a rectangular array. All of the columns must be the same
length, but they may be of different types.
The rows and columns of a matrix or data frame can be given names.
However these are implemented differently in R; many operations will work for one
but not both.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Matrix Operations
> x <- 1:10
> y <- rnorm (10) # generate 10 standard normal random obseravtions
> # make a matrix by column binding two numeric vectors
> mat <- cbind (x , y )
> mat
x y
[1 ,] 1 1.3775675
[2 ,] 2 -0.2749630
. . .
[9 ,] 9 0.1986804
[10 ,] 10 -1.2459279
> # And the names of the rows and columns
> rownames ( mat )
NULL
> colnames ( mat )
[1] " x " " y "
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Matrix Operations
Indexing for matrices works as for vectors except that we now need to include both the
row and column (in that order).
> # The 2 nd element of the 1 st row of mat
> mat [1 ,2]
y
-0.07766897
> # The first ROW of mat
> mat [1 ,]
x y
1.00000000 -0.07766897
> # The first COLUMN of mat
> mat [ ,1]
[1] 1 2 3 4 5 6 7 8 9 10
> # and all elements of mat that are > 4; note no comma
> mat [ mat >4]
[1] 5 6 7 8 9 10
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Matrix Operations
> # create a matrix with 2 columns and 10 rows
> # filled with random normal deviates
> m = matrix ( rnorm (20) , nrow =10)
> # multiply all values in the matrix by 20
> m = m * 20
> # and add 100 to the first column of m
> m [ ,1] = m [ ,1] + 100
> # summarize m
> summary ( m )
V1 V2
Min . : 50.17 Min . : -36.881
1 st Qu .:102.92 1 st Qu .: -24.470
Median :111.41 Median : -9.602
Mean :108.95 Mean : -9.357
3 rd Qu .:121.45 3 rd Qu .: 2.358
Max . :156.64 Max . : 20.467
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Basic Plot Functions
The command plot(x, y) will plot vector x as the independent variable and
vector y as the dependent variable.
Within the command line, you can specify the title of the graph, the name of the
x-axis, and the name of the y-axis.
main=‘title’
xlab=‘name of x axis’
ylab=‘name of y axis’
The command lines(x, y) adds (overlay) a line segment to the plot()
command.
The command points(x, y) adds (overlay) points to the plot() command.
A legend can be created using legend() command.
Demo
> # To see a few examples of plots type the following and
press enter .
> demo ( graphics )
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Basic Plot Functions
Try this yourself:
> x = 1:100
> # 100 random normal deviates with mean =3 , sd =1
> y = rnorm (100 ,3 ,1)
> plot (x , y )
> plot (x ,y , main = ’ My ␣ First ␣ Plot ’)
> # Change point type
> plot (x ,y , pch =3)
> # change color
> plot (x ,y , pch =4 , col =2)
> # draw lines between points
> lines (x ,y , col =3)
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
More Plotting
> z = sort ( y )
> # plot a sorted variable vs x
> plot (x ,z , main = ’ Random ␣ Normal ␣ Numbers ’ ,
+ xlab = ’ Index ’ , ylab = ’ Random ␣ Number ’)
> # another example
> plot ( -4:4 , -4:4)
> # and add a point at (0 ,2) to the plot
> points (0 ,2 , pch =6 , col =12)
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Margin and Outer Margin
You will have full control on the appearance of the plot. You will be able to adjust
margins so that to save space on the page and also that the figure looks attractive and
clear.
> # check margin and outer margin settings
> par ( c ( " mar " , " oma " ))
> plot (x , y )
> par ( oma = c (1 ,1 ,1 ,1)) # set outer margin
> plot (x , y )
> par ( mar = c (2.5 ,2.1 ,2.1 ,1)) # set margin
> plot (x , y )
> # A basic histogram
> hist (z , main = " Histogram " , sub = " Random ␣ normal " )
> # A " density " plot
> plot ( density ( z ) , main = " Density ␣ plot " , sub = " Random ␣ normal " )
> # A smaller " bandwidth " to capture more detail
> plot ( density (z , adjust =0.5) , sub = " smaller ␣ bandwidth " )
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Margin and Outer Margin
# Margins area
par ( oma = c (3 ,3 ,3 ,3)) # all sides have 3 lines of space
par ( mar = c (5 ,4 ,4 ,2) + 0.1)
# Plot : argument type =" n " hides the points , ie a null plot
plot (0:10 , 0:10 , type = " n " , xlab = " X " , ylab = " Y " )
# Place text in the plot and color
# everything plot - related red
text (5 ,5 , " Plot " , col = " red " , cex =2)
box ( col = " red " )
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Margin and Outer Margin
# Place text in the margins and label the margins , all in f o r e s t g r e e n
mtext ( " Margins " , side =3 , line =2 , cex =2 , col = " forestgreen " )
mtext ( " par ( mar = c (b ,l ,t , r )) " , side =3 , line =1 , cex =1 , col = " forestgreen " )
mtext ( " Line ␣ 0 " , side =3 , line =0 , adj =1.0 , cex =1 , col = " forestgreen " )
mtext ( " Line ␣ 1 " , side =3 , line =1 , adj =1.0 , cex =1 , col = " forestgreen " )
mtext ( " Line ␣ 2 " , side =3 , line =2 , adj =1.0 , cex =1 , col = " forestgreen " )
mtext ( " Line ␣ 3 " , side =3 , line =3 , adj =1.0 , cex =1 , col = " forestgreen " )
box ( " figure " , col = " forestgreen " )
# Label the outer margin area and color it blue
# Note the ’ outer = TRUE ’ command moves us from the figure margins to
# the outer margins .
mtext ( " Outer ␣ Margin ␣ Area " , side =1 , line =1 , cex =2 , col = " blue " , outer = TRUE )
mtext ( " par ( oma = c (b ,l ,t , r )) " , side =1 , line =2 , cex =1 , col = " blue " , outer = TRUE )
mtext ( " Line ␣ 0 " , side =1 , line =0 , adj =0.0 , cex =1 , col = " blue " , outer = TRUE )
mtext ( " Line ␣ 1 " , side =1 , line =1 , adj =0.0 , cex =1 , col = " blue " , outer = TRUE )
mtext ( " Line ␣ 2 " , side =1 , line =2 , adj =0.0 , cex =1 , col = " blue " , outer = TRUE )
box ( " outer " , col = " blue " )
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Margin and Outer Margin
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Graphics Devices and Saving Plots
to make a plot directly to a file use: png(), postscript(), etc.
R can have multiple graphics “devices” open.
To see a list of active devices: dev.list()
To close the most recent device: dev.off()
To close device 5: dev.off(5)
To use device 5: dev.set(5)
Save a png image to a file
> png ( file = " myplot . png " , width =480 , height =480)
> plot ( density (z , adjust =2.0) , sub = " larger ␣ bandwidth " )
> dev . off ()
On your own, save a pdf to a file. NOTE: The dimensions in pdf() are in inches
Multiple plots on the same page:
> par ( mfrow = c (2 ,1))
> plot ( density (z , adjust =2.0) , sub = " larger ␣ bandwidth " )
> hist ( z )
> # use dev . off () to turn off the two - row plotting
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Control Structures in R
R has multiple types of control structures that allows for sequential evaluation of
statements.
For loops
for ( x in set ) { operations }
While loops
while ( x in set ) { operations }
If statements (conditional)
if ( condition ) {
some operations
} else { other operations }
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Control Structure and Looping Examples
> x <- 1:9
> length ( x )
> # a simple conditional then two expressions
> if ( length ( x ) <=10) {
+ x <-c (x ,10:20); print ( x )}
> # more complex
> if ( length ( x ) <5) {
+ print ( x )
+ } else {
+ print ( x [5:20])
+ }
> # print the values of x , one at a time
> for ( i in x ) print ( i )
> for ( i in x ) i # note R will not echo in a loop
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Control Structure and Looping Examples
> # loop over a character vector
> y <-c ( ’a ’ , ’b ’ , ’ hi ␣ there ’)
> for ( i in y ) print ( i )
> # and a while loop
> j <-1
> while (j <10) { # do this while j <10
+ print ( j )
+ j <-j +2} # at each iteration , increase j by 2
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Function Overview
Functions are objects and are assigned to names, just like data.
myFunction = function ( argument1 , argument2 ) {
expression1
expression2
}
We write functions for anything we need to do again and again.
You may test your commands interactively at first, and then use the history()
feature and an editor to create the function.
It is wise to include a comment at the start of each function to say what it does
and to document functions of more than a few lines.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Function Overview
> add1 = function ( x ) {
+
# this function adds one to the first argument and returns it
+
x + 1
+ }
> add1 (17)
[1] 18
> add1 ( c (17 ,18 ,19 ,20))
[1] 18 19 20 21
You can use the edit() function to make changes to a function. The following
command will open a window, allow you to make changes, and assign the result to a
new function, add2.
> add2 = edit ( add1 )
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Opening a CSV File
We will start our learning with R by tackling a very simple task: opening a CSV
file (Comma Separated Values)
This file contains a data set consisting of 576 temperature readings and I want to
view those values
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Opening a CSV File
Each temperature value within the file represents a different geographic location,
e.g., 36.2 degrees North and 113.8 degrees West
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Opening a CSV File (Windows)
The name of the file is "surftemp1.csv"
Windows uses the file name suffix (the ".csv") to determine that Excel will open
this file (on a double-click)
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Opening a CSV File (Windows)
Windows uses the file name suffix (the ".csv") to determine that Excel will open
this file (on a double-click)
We can also explicitly decide to use Excel to open the file (first start Excel and
then open the CSV file from Excel).
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Opening a CSV File (R)
We can also open the file using R
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
Opening a CSV File (R)
We will usually just show the R code (and output) rather than the complete R
program
> read . csv ( " surftemp1 . csv " )
X113 .8 W X111 .2 W X108 .8 W X106 .2 W X103 .8 W X101 .2 W
36.2 N 272.7 270.9 270.9 269.7 273.2 275.6
33.8 N 279.5 279.5 275.0 275.6 277.3 279.5
31.2 N 284.7 284.7 281.6 281.6 280.5 282.2
28.8 N 289.3 286.8 286.8 283.7 284.2 286.8
26.2 N 292.2 293.2 287.8 287.8 285.8 288.8
23.8 N 294.1 295.0 296.5 286.8 286.8 285.2
We will not always show ALL of the output
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Concepts
We type an R expression, then R evaluates the expression, then R prints out
the result of the evaluation
In this case, the expression, read.csv("surftemp1.csv"), is a function call
As with other computer languages, we must get the syntax of R code correct first.
We also need to learn the semantics of the R code that we are writing.
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Syntax
A function call consists of the name of the function followed by one or more
arguments within parentheses
function call: read.csv("surftemp1.csv")
function name: read.csv("surftemp1.csv")
parentheses: read.csv("surftemp1.csv")
argument: read.csv("surftemp1.csv")
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Semantics
The read.csv() function can be used to open a CSV file and show the contents of
the file on screen
The first argument to the read.csv() function is a character value that specifies
the file name
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Concepts
The argument to the function call is also an R expression
In this case, the expression, "surftemp1.csv", is a simple character value
expression
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Syntax
A character value expression is anything between double quotes
character value: "surftemp1.csv"
double quotes: "surftemp1.csv"
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Concepts
How does R know the location of the file "surftemp1.csv"?
R has a working directory and it will look there for files
You can set the working directory
> setwd ( " C : / Users / Asad / Desktop " )
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Syntax
That’s another function call!
(and another character value)
function call: setwd("C:/Users/Asad/Desktop")
function name: setwd("C:/Users/Asad/Desktop")
parentheses: setwd("C:/Users/Asad/Desktop")
argument: setwd("C:/Users/Asad/Desktop")
character value: "C:/Users/Asad/Desktop"
double quotes: "C:/Users/Asad/Desktop"
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Semantics
The setwd() function can be used to change the working directory
The first argument to the setwd() function is a character value that specifies the
new working directory
> setwd ( " C : / Users / Asad / Desktop " )
> setwd ( " .. " )
> setwd ( " Desktop " )
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
R Working Directory
As the first argument in a call to the read.csv() function, we can also specify the
file location as a file path, which includes the location of the file as well as the file
name
> read . csv ( " C : / Users / Asad / Desktop / surftemp1 . csv " )
file path: C:/Users/Asad/Desktop/surftemp1.csv
file location: C:/Users/Asad/Desktop/surftemp1.csv
file name: C:/Users/Asad/Desktop/surftemp1.csv
Prof. Dr. Asad Ali Introduction to R Spring 2023
R introduction
EXERCISE
Write at least two different pieces of R code (one or more R expressions) that
will do exactly the same thing as the following piece of R code:
setwd ( " a : / b / c " )
read . csv ( " data . csv " )
Prof. Dr. Asad Ali Introduction to R Spring 2023