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

M1 Basics R Language

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

M1 Basics R Language

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

The Basics of

R Language
Nurwatik,S.T., M.Sc
Contents

Basic use of R and R help


How to give R commands
R data structures
Reading and writing data
Some more R commands (exercises)

Spatial Programming 11/4/2022 2


Preparation

Download R and R Studio in


this website
http://www.r-project.org

Spatial Programming 11/4/2022 3


R Studio GUI
Function and Commands
✓ To use a function in a package, the package needs to be
loaded in memory.
✓ Command for this is library( ), for example:
library(affy)
✓ There are three parts in a command:
✓ the command
✓ brackets
✓ Arguments inside brackets (these are not always present)

Spatial Programming 11/4/2022 5


Function and Commands
+ R is case sensitive, for instance;
library(affy) works, but Library(affy) does not.
+ Multiple commands can be written on the same line.
Here we first remove missing values from the variable
year, and then calculate it’s arithmetic average.
Writing:
+ na.omit(year)
+ mean(year)
Would be the same as
+ mean(na.omit(year))

Spatial Programming 11/4/2022 6


Function and Commands
+ Command can have many arguments. These
are always giving inside the brackets.
+ Numeric (1, 2, 3…) or logic (T/F) values and
names of existing objects are given for the
arguments without quotes, but string values,
such as file names, are always put inside
quotes. For example:
+ mas5(dat3, normalize=T, analysis=”absolute”)

Spatial Programming 11/4/2022 7


Data structures 1/6

+ Vector
A list of numbers, such as (1,2,3,4,5)
R: a<-c(1,2,3,4,5)
+ Command c creates a vector that is assigned to object a
+ Factor
A list of levels, either numeric or string
R: b<-as.factor(a)
+ Vector a is converted into a factor

Spatial Programming 11/4/2022 8


Data structures 2/6
+ Data frame
A table where columns can contain numeric and string values
R: d<-data.frame(a, b)
+ Matrix
All columns must contain either numeric or string values, but
these can not be combined
R: e<-as.matrix(d)
+ Data frame d is converted into a matrix e
R: f<-as.data.frame(e)
+ Matrix e is converted into a dataframe f
Spatial Programming 11/4/2022 9
Data structures 3/6
+ List
Contains a list of objects of possibly different types.
R: g<-as.list(d)
+ Converts a data frame d into a list g

Spatial Programming 11/4/2022 10


Data structures 4/6
+ Some command need to get, for example, a matrix,
and do not accept a data frame. Data frame would give
an error message.
+ To check the object type:
R: class(d)
+ To check what fields there are in the object:
R: d
R: str(d)
+ To check the size of the table/matrix:
R: dim(d)
+ To check the length of a factor of vector:
R: length(a)
Spatial Programming 11/4/2022 11
Data structures 5/6
+ Some data frame related commands:
R: names(d)
+ Reports column names
R: row.names(d)
+ Reports row names
+ These can also be used for giving the names for the data
frame. For example:
R: row.names(d)<-c("a","b","c","d","e")
+ Letters from a to e are used as the row names for data frame d
+ Note the quotes around the string values!
R: row.names(d)

Spatial Programming 11/4/2022 12


Objects

+ Results of calculations can be stored in objects using the


assignment operators:
An arrow (<-) formed by a smaller than character and a hyphen without a space!
The equal character (=).

Spatial Programming 11/4/2022 13


Objects
+ These objects can then be used in other calculations. To
print the object just enter the name of the object. There
are some restrictions when giving an object a name:
Object names cannot contain `strange' symbols like !, +, -, #.
A dot (.) and an underscore ( ) are allowed, also a name starting
with a dot.
Object names can contain a number but cannot start with a
number.
R is case sensitive, X and x are two different objects, as well as
temp and temP.

Spatial Programming 11/4/2022 14


Reading data 1/2

+ Command for reading in text files is:


read.table(”suomi.txt”, header=T, sep=”\t”)
+ This examples has one command with three arguments: file
name (in quotes), header that tells whether columns have titles,
and sep that tells that the file is tab-delimited.
Reading data 2/2
+ It is customary to save the data in an object in R. This is
done with the assignment operator (<-):
dat<-read.table(”suomi.txt”, header=T, sep=”\t”)
+ Here, the data read from file suomi.txt is saved in an
object dat in R memory.
+ The name of the object is on the left and what is assigned
to the object is on the right.
+ Command read.table( ) creates a data frame.
Using data frames
+ Individual columns in the data frame can be accessed using one of
the following ways:
Use its name:
+ dat$year
+ dat is the data frame, and year is the header of one of its columns. Dollar sign ($) is an opertaor that
accesses that column.
Split the data frame into variables, and use the names directly:
+ attach(dat)
+ year
Use subscripts
Subscripts 1/2
+ Subscripts are given inside square brackets after
the object’s name:
dat[,1]
+ Gets the first column from the object dat
dat[,1]
+ Gets the first row from the object dat
dat[1,1]
+ Gets the first row and it’s first column from the object dat
+ Note that dat is now an object, not a command!
Subscripts 2/2
+ Subscripts can be used for, e.g., extracting a subset of the
data:
dat[which(dat$year>1900),]
+ Now, this takes a bit of pondering to work out…
+ First we have the object dat, and we are accessing a part of it, because it’s name is followed by the
square brackets
+ Then we have one command (which) that makes an evaluation whether the column year in the
object dat has a value higher than 1900.
+ Last the subscript ends with a comma, that tells us that we are accessing rows.
+ So this command takes all the rows that have a year higher 1900 from the object dat that is a data
frame.
Writing tables
+ To write a table:
write.table(dat, ”dat.txt”, sep=”\t”)
Here an object dat is written to a file called dat.txt. This file should be tab-delimited (argument sep).
+ To capture what is written on the screen:
sink(”output.txt”)
dat
sink( )
Here, output written on the screen should be written to a file output.txt instead. Contents of the
object dat are written to the named file. Last, the file is closed.
Note that if you accidentally omit the last command, you’ll not be able to see any output on the
screen, because output is still redirected to a file!
Spatial Programming 11/4/2022 21
Script Console

Spatial Programming 11/4/2022 22


Objects
+ To list the objects that you have in your current R session use the
function ls or the function objects.
> ls()
[1] "x" "y"
+ So to run the function ls we need to enter the name followed by an
opening ( and a closing ). Entering only ls will just print the object,
you will see the underlying R code of the the function ls. Most
functions in R accept certain arguments. For example, one of the
arguments of the function ls is pattern. To list all objects starting
with the letter x:
> x2 = 9
> y2 = 10
> ls(pattern="x")
[1] "x" "x2"

Spatial Programming 11/4/2022 23


Objects
+ If you assign a value to an object that already exists then
the contents of the object will be overwritten with the
new value (without a warning!). Use the function rm to
remove one or more objects from your session.
> rm(x, x2)

+ Lets create two small vectors with data and a scatterplot.


z2 <- c(1,2,3,4,5,6)
z3 <- c(6,8,3,5,7,1)
plot(z2,z3)
title("My first scatterplot")

Spatial Programming 11/4/2022 24


Matrices

Spatial Programming 11/4/2022 25


Matrices

Spatial Programming 11/4/2022 26


Read Data

Spatial Programming 11/4/2022 27


Read Data

Spatial Programming 11/4/2022 28


Exercise

- Make a scatterplot from objects


- Make several matrix operations using
R
- Read data from *txt

Spatial Programming 11/4/2022 29


Thank you

Spatial Programming 11/4/2022 30

You might also like