0% found this document useful (0 votes)
40 views22 pages

Ntroductory Tatistics: by Dr. Laila M. Fatehy

The document provides an introductory overview of the R programming language, detailing its strengths, weaknesses, installation instructions, and basic syntax. It covers the use of R for statistical analysis, including arithmetic and logical operators, as well as the creation and manipulation of vectors. Additionally, it introduces Tinn-R, a text editor for managing R scripts, and outlines essential functions and commands for data analysis.

Uploaded by

Hazem Hesham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views22 pages

Ntroductory Tatistics: by Dr. Laila M. Fatehy

The document provides an introductory overview of the R programming language, detailing its strengths, weaknesses, installation instructions, and basic syntax. It covers the use of R for statistical analysis, including arithmetic and logical operators, as well as the creation and manipulation of vectors. Additionally, it introduces Tinn-R, a text editor for managing R scripts, and outlines essential functions and commands for data analysis.

Uploaded by

Hazem Hesham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Faculty of Science

INTRODUCTORY STATISTICS
with

By

Dr. Laila M. Fatehy

Mathematics & Computer Science Department


Faculty of Science
Alexandria University

0
1
Elementary Introduction To

R is an integrated suite of software facilities for data manipulation, calculation


and graphical display. Most classical and modern statistical techniques have been
implemented and much of the latest methodology is available for use with R, but users
may need to be prepared to do a little work to find it.

1.1 R Strengths and Weaknesses


Strengths
 Free and open source, supported by a strong user community
 Implementation of modern statistical methods
 Comparable, and often superior, in power to commercial products in most senses
 Available for windows, macs, linux
 In addition to enabling statistical operations, it‟s a general programming
language, so that you can automate your analyses and create new functions
 Object-oriented and functional programming structure
 Your data sets are saved between sessions, so you don‟t have to reload each time
 Open-software nature means it‟s easy to get help from the user community

Weaknesses
 Slow or impossible with large data sets
 Non-standard programming paradigms

1
1.2 Obtaining and Installing R
 Latest Copy
The latest copy of R can be downloaded from the CRAN (Comprehensive
R Archive Network) website:

http://cran.r-project.org/
As of this writing, the version for recent variants of Microsoft Windows comes
as a single R-3.1.2-win32.exe file, on which you simply double-click with the mouse
and then follow the on-screen instructions. When the process is completed, you will
have an entry under Programs on the Start menu for invoking R, as well as a desktop
icon.

The ready prompt is ">".

 R Packages
R packages can also be downloaded from this site or alternatively, they can be
obtained via R once the package has been installed. A list of R packages
accompanied by a brief description can be found on the website itself.

 Installing R Packages
The installation and loading of R packages can be done within R by going up to
the Packages menu and clicking on Install package(s) from CRAN. A dialog box
will appear with a list of available packages to install.

Alternatively, the install.packages ( ) function can be used from the command


line.

 Loading R Packages
Once the packages have been installed, they can be loaded into R. Go to the
Packages menu and select Load package. Select the package that is required and
click on OK. These packages should be loaded into your current R session.

2
Alternatively, the functions library( ) can be used to load installed packages
into R.

 Updating R Packages
Updating R packages can be achieved either through the menu or by using the
function update.packages( ) at the command line.

 Quitting R
To quit from R either type q( ) in the R console or commands window or
alternatively just shut the window. You will be prompted whether you want to save
your session. Most times you will answer yes to this

1.3 Tinn R Editor


 Tinn-R is a free text editor that runs under Windows (9X/Me/2000/XP) to manage
your R scripts, useful when you are editing or managing many lines of R codes or
routines.
 It recognizes R commands/functions, does automatic matching of parentheses,
and has icons that execute R script you create in Tinn-R.
 An advantage of using Tinn-R is that you can preserve the R script you created to
do a survey design or survey analysis. If your input data changes, it is simple to
redo an entire analysis without changing any of the R script.

 Download Tinn-R from:

https://sourceforge.net/projects/tinn-r
 Once you open Tinn-R, you may start writing R script or editing what you have in
the file opened by Tinn-R.
 Tinn-R will be able to recognize that an R session has been opened.

3
Some tools or features available in Tinn-R are:

1. You can highlight a section or "block" of your R scripts, and send them into R.
You can even send the whole scripts written in your 'project' to R.
2. Open several files of R scripts, and browse through them between tabs.
3. Editing and formatting tools, including commenting/un-commenting a block of
scripts.
4. Delimiter checks. This can be very useful when you are running a lot of codes
involving many nested loops.
5. Find/Search and replace tools that can search across files.

1.4 Arithmetic and Logical Operators

Operators Operator Description Example Output


+ Addition > 2+5 [1] 7
- Subtraction, sign > 2-5 [1] -3
* Multiplication > 2*5 [1] 10
/ Division > 2/5 [1] 0.4
^ Raise to power (Exponentiation) > 2^5 [1] 32
%/% Integer division > 5%/%2 [1] 2

%% Remainder from integer division 5%%2 [1] 1

Table 1.1: Arithmetical Operators

4
Operator Logical Descreption
== Equals
!= Not equals
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
is.na(x) Missing?
& Logical AND
| Logical OR
! Logical NOT
Table 1.2: Logical and Relational Operators

Mathematical

log(x) Logarithm of x, natural (base-e) logarithm


log10(x) Base-10 logarithm
exp(x) Exponential function ex
sin(x) Sine
cos(x) Cosine
tan(x) Tangent
asin(x), acos(x), atan(x) Arcsin (inverse sine) , (inverse cosine), (inverse tangent)
min(x) Smallest value in vector
min(x1,x2,...) Minimum over several vectors (one number)
max(x) Largest value in vector
range(x) Like c(min(x),max(x))
pmin(x1,x2,...) Parallel (elementwise) minimum over multiple equally long vectors
pmax(x1,x2,...) Parallel maximum
length(x) Number of elements in vector
sum(complete.cases(x)) Number of nonmissing elements in vector
Rounding
round
abs Absolute value
ceiling, floor, trunc Truncating to integer values
sqrt Square root
Table 1.3: Numerical Functions

5
Statistical
mean(x) Average
sd(x) Standard deviation
var(x) Variance
median(x) Median
quantile(x,p) Quantiles
cor(x,y) Correlation
Table 1.4: Statistical Functions

1.5 The R Language: Basic Syntax


It is important to learn some basic syntax of the R programming language before
launching into more sophisticated functions, graphics and modelling.

 The default R prompt is the greater-than sign (>)

 R is case sensitive, so A and a are different symbols and would refer to different
variables.
 Commands are separated either by a semi-colon („;‟), or by a newline .

 Elementary commands can be grouped together into one compound expression by


braces („{‟ and „}‟).
 Comments can be put almost anywhere, starting with a hashmark („#‟), everything
to the end of the line is a comment.

 If a command is not complete at the end of a line, R will give a different prompt,
by default + on second and subsequent lines and continue to read input until the
command is syntactically complete. This prompt may be changed by the user. We
will generally omit the continuation prompt and indicate continuation by simple
indenting.
 Some variable names are already used by the system. This can cause some
confusion if you use them for other purposes. The worst cases are the single-letter
names c, q, t, C, D, F, I, and T, but there are also diff, df, and pt, for example. Most
of these are functions and do not usually cause trouble when used as variable
names.

6
 Getting help with functions and features
R has an inbuilt help facility. To get more information on any specific named
function, for example mean, the command is

> help(mean)
An alternative is
> ?mean
For a feature specified by special characters, the argument must be enclosed in
double or single quotes, making it a “character string”: This is also necessary for a
few words with syntactic meaning including if, for and function.

> help("mean")

1.5.1 Overgrown Calculator


To enter and receive the result of the arithmetic expression
5e2 (cos(2 / 6)  tan1 (1.0))
> 5*exp(-2)*(cos(2*pi/6)+atan(1.0))
[1] 0.8697986
>2*
+4
[1] 8

Now to calculate (5x2-3x+4) say, you can enter


> 5*x^2-3*x+4
[1] 18

1.5.2 Assignment
The standard assignment operator in R is <-. However, there are also ->, = and
even the assign( ) function.

To assign the value 2 to the variable x, you can enter

> x <- 2

7
Assignments
<- Assign value to variable
-> Assignment “to the right”
<<- Global assignment (in functions)

 A data or a numeric vector (an array of numbers) can be assigned using the
function c( ) which is a short for “concatenate”, joining items end to end.

Example: To set up a vector named x, say, consisting of five numbers, namely


10.4, 5.6, 3.1, 6.4 and 21.7, use the R command

> x <- c(10.4, 5.6, 3.1, 6.4, 21.7)


>x
[1] 10.4 5.6 3.1 6.4 21.7

An equivalent ways of making the same assignment as above are with:

> assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))

> c(10.4, 5.6, 3.1, 6.4, 21.7) -> x

Random generation from the standard normal distribution by using the function
rnorm( )
> rs6 <- rnorm(6) #random generation for a random sample of size 6 from the standard
normal distribution
> rs6
[1] -0.58307171 -0.34253325 0.35135449 -1.22851130 0.02729377 -1.61283420

> xbar <- sum(x)/length(x)


> xbar

[1] 9.44

> mean(x)

[1] 9.44

8
> sd(x)
[1] 7.33846

> sqrt(sum((x-xbar)^2)/(length(x)-1))
[1] 7.33846

 A character vector is a vector of text strings, whose elements are specifid and
printed in quotes:

> c( "Jan","Apr", "Jul", "Oct")


[1] "Jan" "Apr" "Jul" "Oct"

 Logical vectors can take the value TRUE or FALSE (or NA; see below). In input,
you may use the convenient abbreviations T and F (if you are careful not to
redefine them). Logical vectors are constructed using the c( ) function just like the
other vector types:

> c(T,T,F,T)
[1] TRUE TRUE FALSE TRUE

Actually, you will not often have to specify logical vectors in the manner above. It
is much more common to use single logical values to turn an option on or off in a
function call. Vectors of more than one value most often result from relational
expressions:
> x > 90
[1] FALSE FALSE FALSE FALSE TRUE TRUE

1.5.3 Vectors
There are three functions, c, seq, and rep, that are used to create vectors in various
situations.

1.5.3.1 c function

short for “concatenate”, joining items end to end


> c (42,57,12,39,1,3,4)
[1] 42 57 12 39 1 3 4

9
 You can also concatenate vectors of more than one element as in

> x <- c(1, 2, 3)


> y <- c(10, 20)
> c(x, y, 5)
[1] 1 2 3 10 20 5

 It is also possible to assign names to the elements. This modifies the way the vector
is printed and is often used for display purposes.
> x <- c(s1="Jan", s2="Feb", s3="Mar")
>x
s1 s2 s3
"Jan" "Feb" "Mar"

 The names can be extracted or set using the function names():


> names(x) <- x
> names(x)
[1] "Jan" "Feb" "Mar"

 All elements of a vector have the same type. If you concatenate vectors of different
types, they will be converted to the least “restrictive” type:

> c(FALSE, 3)
[1] 0 3
> c(pi, "abc")
[1] "3.14159265358979" "abc"
> c(FALSE, "abc")
[1] "FALSE" "abc"

1.5.3.2 seq function


short for “sequence”, is used for equidistant series of numbers.
> seq(1,12) # yields integers from 1 to 12
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> 1:12 # exactly the same as seq(1,12), only easier to read
[1] 1 2 3 4 5 6 7 8 9 10 11 12

10
> seq(1,12,2) # yields a sequence from 1 to 12 in jumps of 2
[1] 1 3 5 7 9 11
> seq(-1,1,0.2)
[1] -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0

>letters[10:15] # to generate a sequence of letters

1.5.3.3 rep function


short for “replicate”, is used to generate repeated values. It is used in
two variants, depending on whether the second argument is a vector or a
single number as:
> h <- c(seq(1,5))
> rep(h,3)
[1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

> t <- c(7,9,13)


> rep(t,3) # repeats the entire vector t three times
[1] 7 9 13 7 9 13 7 9 13

> rep(t,1:3)
[1] 7 9 9 13 13 13

 In the previous function the argument 1:3 is a vector with the three values (1, 2,
3); these values correspond to the elements of the t vector, indicating that 7
should be repeated once, 9 twice, and 13 three times.

 The rep function is often used for things such as group codes: If it is known that
the first 10 observations are men and the last 15 are women, you can use the next
line to form a vector that for each observation indicates whether it is from a man
or a woman

> rep(1:2,c(10,15))
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

11
 The special case where there are equally many replications of each value
can be obtained using the each argument. E.g., rep(1:2,each=10) is the same
as rep(1:2,c(10,10)).

> rep(1:2,each=10)
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2

> rep(1:2,c(10,10))

1.5.3.4 paste function


takes an arbitrary number of arguments and concatenates them one by
one into character strings

> labs <- paste(c("X","Y"), 1:10, sep="")


[1] "X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"

1.5.4 Matrices and Arrays


Matrices are just a two-dimensional array of numbers, are used for many
purposes in theoretical and practical statistics.
Matrices and arrays are represented as vectors with dimensions:
> m <- 1:12
> dim(m) <- c(3,4)
>m
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

 A convenient way to create matrices is to use the matrix function:


> matrix(1:12,nrow=3)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

 Notice how the byrow=T switch causes the matrix to be filled in a rowwise fashion
rather than columnwise.
> matrix(1:12,nrow=3,byrow=T)

12
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12

> matrix(c(10,12,14,20,22,24),nrow=2,byrow=T)
[,1] [,2] [,3]
[1,] 10 12 14
[2,] 20 22 24

 The transposition function t (notice the lowercase t as opposed to uppercase T for


TRUE), which turns rows into columns and vice versa:
> z <- matrix(c(10,12,14,20,22,24),nrow=2,byrow=T)
>z

[,1] [,2] [,3]


[1,] 10 12 14
[2,] 20 22 24
> t(z)
[,1] [,2]
[1,] 10 20
[2,] 12 22
[3,] 14 24

 The brackets [ ] can be used to select an element, a row or a column, e.g.


> z[2,2]
[1] 22
> z[2,]
[1] 20 22 24
> z[,2]
[1] 12 22
> z[,1:2]
>dim(z) # check dimension of z
> length (z)

13
 colMeans() , rowMeans() to find the means of the columns and rows
respectively of matrices.
 Diagonal diag( )

 The meaning of diag( ) depends on its argument. diag(v), where v is a vector,


gives a diagonal matrix with elements of the vector as the diagonal entries.

 On the other hand diag(M), where M is a matrix, gives the vector of main
diagonal entries of M.

 If k is a single numeric value then diag(k) is the k by k identity matrix.

> x<- c(10,20,30,40)


> diag(x)
[,1] [,2] [,3] [,4]
[1,] 10 0 0 0
[2,] 0 20 0 0
[3,] 0 0 30 0
[4,] 0 0 0 40
> diag(2:4)
[,1] [,2] [,3]
[1,] 2 0 0
[2,] 0 3 0
[3,] 0 0 4

> h<-matrix(c(1,2,0,-2,-7,8,-5,8,8),nrow=3,byrow=T) ; print(h)


[,1] [,2] [,3]
[1,] 1 2 0
[2,] -2 -7 8
[3,] -5 8 8
> diag(h)
[1] 1 -7 8

>sum(diag(h)) # to find the trace of matrix h


[1] 2

> diag(7) # to create an identity matrix of order 7

14
 Combining Matrices by Joining Rows & Joining Columns

 Joining Rows , rbind()


You can combine several matrices with the same number of columns by joining
them as rows, using the rbind() command, here is an example:

> A <- matrix(c (1 ,3 ,3 ,9 ,6 ,5) ,2 ,3)


> B <- matrix(c (9 ,8 ,8 ,2 ,9 ,0) ,2 ,3)
>A
[,1] [,2] [,3]
[1,] 1 3 6
[2,] 3 9 5
>B
[,1] [,2] [,3]
[1,] 9 8 9
[2,] 8 2 0

> rbind(A,B)
[,1] [,2] [,3]
[1,] 1 3 6
[2,] 3 9 5
[3,] 9 8 9
[4,] 8 2 0
> rbind(B,A)
[,1] [,2] [,3]
[1,] 9 8 9
[2,] 8 2 0
[3,] 1 3 6
[4,] 3 9 5
 If A and B have the same number of columns. To combine these by rows into a
matrix z, together with an initial rows of 1s we can use
> z <- rbind(1, A, B)
>z
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 3 6
[3,] 3 9 5
[4,] 9 8 9
[5,] 8 2 0

15
 Joining Columns , cbind()
In similar fashion, you can combine several matrices with the same number of rows
by joining them as columnss, using the cbind() command

 Determinant, det()
R has a builtin function det() to calculate a determinant
> E <- matrix(c(6,-1,2,3,4,-5, 1,0,-2),nrow=3); E
[,1] [,2] [,3]
[1,] 6 3 1
[2,] -1 4 0
[3,] 2 -5 -2

> det(E)
[1] -57

 The outer product of two arrays

An important operation on arrays is the outer product. If a and b are two numeric
arrays, their outer product is an array whose dimension vector is obtained by
concatenating their two dimension vectors (order is important), and whose data
vector is got by forming all possible products of elements of the data vector of a
with those of b. The outer product is formed by the special operator %o%:
> a<-c(1,2,3)
> b<-c(4,5,6,7)
> ab<-a%o%b
> ab
[,1] [,2] [,3] [,4]
[1,] 4 5 6 7
[2,] 8 10 12 14
[3,] 12 15 18 21

An alternative is

> ab <- outer(a, b, "*")

16
 The multiplication function can be replaced by an arbitrary function of two
variables.
For example: if we wished to evaluate the function f(x; y) = cos(y)/(1 + x2) over
a regular grid of values with x- and y-coordinates defined by the R vectors x and
y respectively, we could proceed as follows:
> x <- c(1:10)
> y <- c(0:pi,0.2)
> f <- function(x, y) cos(y)/(1 + x^2)
> z <- outer(x, y, f)
>z
[,1] [,2] [,3] [,4] [,5]
[1,] 0.500000000 0.270151153 -0.208073418 -0.494996248 0.490033289
[2,] 0.200000000 0.108060461 -0.083229367 -0.197998499 0.196013316
[3,] 0.100000000 0.054030231 -0.041614684 -0.098999250 0.098006658
[4,] 0.058823529 0.031782489 -0.024479226 -0.058234853 0.057650975
[5,] 0.038461538 0.020780858 -0.016005648 -0.038076634 0.037694868
[6,] 0.027027027 0.014602765 -0.011247212 -0.026756554 0.026488286
[7,] 0.020000000 0.010806046 -0.008322937 -0.019799850 0.019601332
[8,] 0.015384615 0.008312343 -0.006402259 0.015230654 0.015077947
[9,] 0.012195122 0.006589053 -0.005074961 -0.012073079 0.011952031
[10,] 0.009900990 0.005349528 -0.004120266 -0.009801906 0.009703629

 Matrix multiplication

The operator %*% is used for matrix multiplication. If, for example, A and B are
square matrices of the same size, then
>A*B is the matrix of element by element products, and
> A %*% B is the matrix product.
 Example:
 6 4 3  1 1   2 3 
     
If C   3 9 7  , D   2 0  , A   5 0  ,then
 8 5 2   3 1  3 5 
     

17
 6(1)  4( 2)  3(3) 6(1)  4(0)  3( 1)   5 9 
   
CD   3(1)  9( 2)  7(3) 3(1)  9(0)  7( 1)    0 10 
 8(1)  5( 2)  2(3) 8(1)  5(0)  2( 1)  12 10 
   

> C <- matrix(c(6,4,-3,3,9,-7,8,5,-2), nrow=3, byrow=T)


D <- matrix(c(1,1,2,0,3,-1), nrow=3, byrow=T)
A <- matrix(c(2,-3,5,0,3,-5), nrow=3, byrow=T)
CD <- C%*%D
[,1] [,2]
[1,] 5 9
[2,] 0 10
[3,] 12 10
> D*A
[,1] [,2]
[1,] 2 -3
[2,] 10 0
[3,] 9 5
>D%*%A
Error in D %*% A : non-conformable arguments

 Crossproducts

The function crossprod() forms “crossproducts”, meaning that crossprod(x, y) is


the same as t(x) %*% y but the operation is more efficient. If the second argument
to crossprod() is omitted it is taken to be the same as the first.

 Examples

> crossprod(1:4) # = sum(1 + 2^2 + 3^2 + 4^2)


[,1]
[1,] 30
> crossprod(1:4,4:7) # = sum(1*4 + 2*5 + 3*6 + 4*7)
[,1]
[1,] 60

 An alternative is:
> a<-1:4 # a<-seq(1,4)
> b<-4:7 # b<-seq(4,7)

18
> crossprod(a) # = sum(1 + 2^2 + 3^2 + 4^2)
[,1]
[1,] 30
> crossprod(b) # = sum(4*4 + 5*5 + 6*6 + 7*7)
[,1]
[1,] 126
> crossprod(a,b) # = sum(1*4 + 2*5 + 3*6 + 4*7)
[,1]
[1,] 60

 Matrix inversion

Solving linear equations is the inverse of matrix multiplication. When after

> b <- A %*% x


only A and b are given, the vector x is the solution of that linear equation system. In R,

> solve(A,b)
solves the system, returning x (up to some accuracy loss).

Example : to solve
x+y = 5
x –y = 1

> solve(matrix(c(1,1,1,-1),2,2,byrow=T),c(5,1))
[1] 3 2

 Note that in linear algebra, formally x = A-1b where A-1 denotes the inverse of A,
which can be computed by > solve(A), for example;

> w <- matrix(c(10,2,1,2,20,6, 1,6,30),nrow=3)

> solve(w)
[,1] [,2] [,3]
[1,] 0.102099928 -0.009775525 -0.001448226
[2,] -0.009775525 0.054127444 -0.010499638
[3,] -0.001448226 -0.010499638 0.035481535

19
 Numerically, it is both inefficient and potentially unstable to compute x
<- solve(A) %*% b instead of <- solve(A,b)
 The quadratic form xT A-1x which is used in multivariate computations, should be
computed by something like t(x)%*% solve(A,x), rather than computing the
inverse of A.

 Eigenvalues and eigenvectors


The function eigen(w) calculates the eigenvalues and eigenvectors of a symmetric
matrix w. The result of this function is a list of two components named values and
vectors. The assignment
> ev <- eigen(w)
will assign this list to ev. Then ev$val is the vector of eigen values of w and ev$vec
is the matrix of corresponding eigenvectors.
> ev <- eigen(w)
> ev$val
[1] 32.94503 17.44151 9.61346

> ev$vec
[,1] [,2] [,3]
[1,] -0.07662051 -0.1785530 0.980942473
[2,] -0.42900733 -0.8822029 -0.194089574
[3,] -0.90004556 0.4357028 0.009005696

 Had we only needed the eigenvalues we could have used the assignment:
> evals <- eigen(w)$values
evals now holds the vector of eigenvalues and the second component is
discarded. If the expression:
> eigen(w)
is used by itself as a command the two components are printed, with their names.

 For large matrices it is better to avoid computing the eigenvectors if they are not
needed by using the expression
> evals <- eigen(w, only.values = TRUE)$values

20
1.6 Managing Your Objects
1.6.1 Listing Your Objects with the ls() Function

The ls() command will list all of your current objects. The entities that R creates
and manipulates are known as objects. These may be variables, arrays of numbers,
character strings, functions, or more general structures built from such components.
Write the R command:
> ls () Or alternatively > objects ()
[1] "fn" "k" "x" "y" "z" "A" "x1"

1.6.2 Removing Specified Objects with the rm() Function


To remove objects you no longer need, use rm(). For instance,
> rm(y,z)
> ls ()
[1] "fn" "k" "x" "A" "x1"

 All objects created during an R sessions can be stored permanently in a file for use
in future R sessions. At the end of each R session you are given the opportunity to
save all the currently available objects. If you indicate that you want to do this, the
objects are written to a file called ‘.RData’ in the current directory, and the
command lines used in the session are saved to a file called ‘.Rhistory’.
 When R is started at later time from the same directory it reloads the workspace
from this file. At the same time the associated commands history is reloaded.

1.6.3 Getting the working directory with the getwd() Function


Returns an absolute filepath representing the current working directory of the R
process
>getwd()
[1] "C:/Users/Documents"

1.6.4 Setting the working directory with the setwd() Function


Is used to set the working directory to „work‟.
> setwd(“D:\\work”)

21

You might also like